红宝石在轨道上 - RSpec 错误"undefined method 'respond_to' for Rspec::Core::ExampleGroup::Nested"



我有一个bug,我似乎无法精确定位。我有一个现有的项目,我要向它添加测试。当我尝试运行我的测试(只是一个简单的测试…用FactoryGirl构建一个User,并调用"should_respond_to (:email)"),我得到以下错误:

1) User should have an email
   Failure/Error: should respond_to(:email)
   NoMethodError:
     undefined method 'respond_to' for #<Rspec::Core::ExampleGroup::Nested_3:0x00000008355590>
   # ./spec/models/user_spec.rb:7:in 'block(2 levels) in <top (required)>'

我有一个要点,我的代码在这里:
https://gist.github.com/jeffstagg/9477647

我在同一个运行rspec测试的开发盒上有另一个项目,尽管我先用我的测试构建它,然后用rspec/capybara/guard开始这个项目。我稍后会回到这个项目,显然在为它设置rspec时遗漏了一些东西。有人能看出我遗漏了什么吗?

您正在尝试触发RSpec的谓词匹配器,这需要您将其写为be_predicate,例如

cat.can_has_cheezeburger?变为cat.should be_can_has_cheezburger?

有时这些结果非常可读,例如.too_big? => should be_too_big。其他时候,它们不太工作,如.respond_to? => should be_respond_to

对于这种情况,您最好使用.respond_to?.should be_true

添加到您的rails_helper.rb(spec/support/rails_helper.rb)文件:

*

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
*

最新更新