Four-Phase test with RSpec’s raise_error/raise_exception

Greg Lazarev
1 min readSep 3, 2019

--

Have you ever written an RSpec test similar to this?

It’s not clearly obvious which part of this test is the “exercise” and which is the “verify” phase, as outlined by xUnit’s Four-Phase Test.

Additionally, if you use HoundCI or RuboCop to lint your Ruby code, you may see a warning like (with default configuration):

Style/BlockDelimiters: Avoid using {…} for multi-line blocks.

You could switch to do...end block style, but end.to doesn't look very idiomatic.

There is an alternative to writing this, where the phases are more distinct and identified more easily. With the help of Ruby lambda we can rewrite our test as:

With this approach, we can clearly identify the “setup”, “exercise”, and “verify” phases. As a bonus, there’s no need to do syntax gymnastics to please our Hound or RuboCop!

--

--