Alternatives

The should method makes for nice, readable expectations. But it has a downside: it doesn’t play well with unconventional Ruby objects like delegates.[289]

For these cases, RSpec supports a similar notation called expect that isn’t subject to these limitations.

rspec/expect.rb
 
expect(2 + 2).to == 4

Here are a few of the examples from earlier that have been adapted to use expect:

rspec/expect.rb
 
expect(2 + 2).not_to == 5
 
expect(2 + 2).to be > 3
 
 
expect(​'hello'​).to =~ /ell/
 
expect(some_object).to be_festive
 
 
expect {
 
SomeNonExistentClass.new
 
}.to raise_error(NameError)
 
 
expect(this_book).to please(​'developers'​)

While Cucumber works well with rspec-expectations, you’re certainly not required to use it. If you don’t ...

Get Cucumber Recipes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.