简体   繁体   中英

RSpec error “undefined method 'respond_to' for Rspec::Core::ExampleGroup::Nested”

I'm having a bug that I can't seem to pinpoint. I have an existing project that I'm adding tests to. When I try running my test (just a simple test... build a User with FactoryGirl, and calling "should respond_to(:email)"), I get the following error:

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)>'

I have a gist with my code in it here:
https://gist.github.com/jeffstagg/9477647

I've got another project on the same dev box that runs rspec tests just fine, though I built it with my tests first and started the project with rspec / capybara / guard in mind. This project I'm coming back to later, and apparently missed something when setting up rspec for it. Can anyone see where I'm missing something?

You're trying to trigger RSpec's predicate matcher, which requires you write it as be_predicate , eg.

cat.can_has_cheezeburger? becomes cat.should be_can_has_cheezburger?

Sometimes these end up very readable, eg .too_big? => should be_too_big . Other times they don't quite work, like .respond_to? => should be_respond_to .

For such cases you're better off just using .respond_to?.should be_true

Add below to your rails_helper.rb(spec/support/rails_helper.rb) file:

*

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

*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM