Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Relationships

NoMethodError: undefined method…

When I run bin/rspec spec/models/todo_item_spec.rb, it returns:

F

Failures:

1) TodoItem Failure/Error: it { should belong_to(:todo_list) } NoMethodError: undefined method belong_to' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a97eb4> # ./spec/models/todo_item_spec.rb:5:inblock (2 levels) in <top (required)>'

Finished in 0.00135 seconds 1 example, 1 failure

Failed examples:

rspec ./spec/models/todo_item_spec.rb:5 # TodoItem

Randomized with seed 14438

My code for todo_item_spec.rb is:

require 'spec_helper'

describe TodoItem do

    it { should belong_to(:todo_list) }

end

When I run bin/rspec spec/models/todo_list_spec.rb, it returns:

F

Failures:

1) TodoList should have many :todo_items Failure/Error: it { should have_many(:todo_items) } NoMethodError: undefined method has_many?' for #<TodoList:0xb9f42f68> # ./spec/models/todo_list_spec.rb:5:inblock (2 levels) in <top (required)>'

Finished in 0.00893 seconds 1 example, 1 failure

Failed examples:

rspec ./spec/models/todo_list_spec.rb:5 # TodoList should have many :todo_items

Randomized with seed 59687

My code for todo_list_spec.rb is:

require 'spec_helper'

describe TodoList do

    it { should have_many(:todo_items) }

end

Did shoulda-matchers gem get bundled properly? You may need to use gem install shoulda-matchers, then bundle.

Seth, Thank you for your reply.

I attempted the install of the should-matchers gem again, bundled, and then verified the install and location using:

bundle show shoulda-matchers

Versions of gems are:

  • shoulda-matchers 3.0.0
  • rails 4.0.1
  • spec-rails 2.0.
  • capybara 2.1.0

The original errors persist, even after attempting the shoulda-matchers reinstall.

Just FYI -- I'm also getting the same errors, haven't been able to resolve, googled for a while but no luck.

6 Answers

Per the shoulda-matchers docs, you need to add some configuration to your spec_helper.rb. I looked back at my odot, I had shoulda-matchers 2.6.0 and no configuration. This must be new in 3.0. Anyways, you will need:

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

Add that to the end of spec_helper.rb. (Outside of the rspec configure block.) You should get a pass now.

Thank you for finding this! I should have looked harder. :/

Your solution worked (except your config.integrate block is missing a second pipe on the with variable).

So it should look like this:

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

Oops, good catch.

Thanks so much, Seth! That worked! I'll have to be more fearless about combing through the docs. Great find.

Thank you, Megan, for catching the error with the second pipe. Your profile photo update also gets a thumbs up.

i love you guyz! thank you!

Ariel, I'm glad I wasn't the only one with that question.

For anyone still having issues & is using rspec 3.x:

it needs to be added to the end of rails_helper not spec_helper

https://github.com/thoughtbot/shoulda-matchers/issues/803

Quote from this thread that makes ALOT of sence.

"The shoulda-matchers configuration should not be added to spec_helper, but to rails_helper.

When you run the rspec executable, because you have --require spec_helper in .rspec, RSpec will load spec_helper first before it tries to run any files. The goal of spec_helper is to set up any tests that don't need Rails to run; therefore, it will not load your Rails environment or automatically require any gems in your Gemfile, including shoulda-matchers. This is why the Shoulda constant isn't available. To fix this, move that configuration block into rails_helper (which does set up a Rails environment) and everything should be golden."

This depends on the version of rspec. I believe in the course Jason specifies rspec 2.0. In this case there is only a spec_helper.rb fil. In rspec 3.0 you have a spec_helper.rb and rails_helper.rb. In this case all customization does go in rails_helper.rb

I have updated the answer to reflect the rspec version re: @Seth Reece

Thanks so much for your explanation!! That totally fixed my issue!

Thanks so much for your explanation!! That totally fixed my issue!

Hi guys,

I'm using Rspec 3.2.3 but can't find a rails_helper.rb file. I put the updated configuration in the spec_helper.rb file and everything is working correctly but wondering why I'm missing the rails_helper.rb file?

Any thoughts?

WIth shoulda-matchers 3.0.1 an extra configuration step is needed as answered above by Seth.

For reference:

https://github.com/thoughtbot/shoulda-matchers#configuration

For any who is still having issues, I had to include "require 'shoulda/matchers'" in spec_helper.rb, in addition, to the above mentioned code snippet.