Jumbalaya Wanton
Jumbalaya Wanton

Reputation: 1621

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?

Upvotes: 6

Views: 5987

Answers (2)

elreimundo
elreimundo

Reputation: 6276

expect is a bit more universal. You're passing an object or a block to rspec's expect method, rather than attempting to call a should method on objects that are outside of your control. As a result, expect is coming more and more into favor among developers.

Upvotes: 4

Guilherme
Guilherme

Reputation: 1146

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

Upvotes: 9

Related Questions