expect()在rspec/cucumber中做了什么?

Jum*_*ton 5 ruby testing rspec ruby-on-rails cucumber

在Michael Hartl的Rails教程中,许多示例使用了一种expect()方法.这是黄瓜步骤定义中的一个这样的例子:

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

同样的例子也可以写成同样的效果:

Then /^she should see her profile page$/ do
  page.should have_title(@user.name)
end
Run Code Online (Sandbox Code Playgroud)

为何使用expect()?它增加了什么价值?

Gui*_*rme 8

有关于此的rspec-expectations宝石的文档. 为什么切换到应该预期基本上:

应该和不应该通过添加到每个对象来工作.但是,RSpec并不拥有每个对象,也无法确保它们在每个对象上都能保持一致.特别是,与BasicObject-subclassed代理对象一起使用时,它们可能会导致意外失败.期望通过不需要在所有对象上可用来完全避免这些问题.

更详细的原因在于:RSpec的新期望语法