Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does expect() do in rspec/cucumber?

In Michael Hartl's Rails Tutorial, many examples use an expect() method. Here's one such example in a cucumber step definition:

Then /^she should see her profile page$/ do
  expect(page).to have_title(@user.name)
end

This same example can be written as such to the same effect:

Then /^she should see her profile page$/ do
  page.should have_title(@user.name)
end

Why use expect()? What value does it add?

like image 590
Jumbalaya Wanton Avatar asked Nov 18 '13 00:11

Jumbalaya Wanton


1 Answers

There's a documentation on the rspec-expectations gem about this. Why switch over from should to expect Basically:

should and should_not work by being added to every object. However, RSpec does not own every object and cannot ensure they work consistently on every object. In particular, they can lead to surprising failures when used with BasicObject-subclassed proxy objects. expect avoids these problems altogether by not needing to be available on all objects.

A more detailed reasons for this is placed in: RSpec's New Expectation Syntax

like image 133
Guilherme Avatar answered Oct 06 '22 20:10

Guilherme