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 Cleaning Up Our View

Undefined method issue. Completely clueless, please help.

Spent over an hour trying to find the error in my code to fix. Here is the test results:

[dcdevman@DCDevMan odot]$ bin/rspec --format=documentation spec/models/todo_item_spec.rb

TodoItem example at ./spec/models/todo_item_spec.rb:4 (FAILED - 1) #completed? is false when completed_at is blank returns true when completed_at is not empty

Failures:

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

Finished in 0.00911 seconds 3 examples, 1 failure

Failed examples:

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

Randomized with seed 26229

Here is the todo_item_spec.rb file:

require 'spec_helper'

describe TodoItem do it { should belong_to(:todo_list) }

describe "#completed?" do let(:todo_item) { TodoItem.create(content: "Hello") }

it "is false when completed_at is blank" do
  todo_item.completed_at = nil
  expect(todo_item.completed?).to be false
end

it "returns true when completed_at is not empty" do
  todo_item.completed_at = Time.now
  expect(todo_item.completed?).to be true
end

end end

2 Answers

Seth Reece
Seth Reece
32,867 Points

Hi Douglas,

This problem has come up a few times now. If you are using shoulda-matchers 3.0, you need to add some configuration to spec_helper.rb.

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

Add that after the last end in the file. Quite a few people have come across this recently. Perhaps we can get Jason Seifer to add a teachers note to this course.

Rachelle Wood
Rachelle Wood
15,362 Points

Wow, thank you so much! This worked for me. Unfortunately the official documentation's link to configuring Shoulda Matchers with Rspec is broken for me. This really helped me out.

Ok, I added the shoulda-matchers configuration statement to my spec_helper.rb file. Now after I run a test I get this in my terminal:

bin/rspec spec/models/todo_list_spec.rb /home/dcdevman/Documents/devlang/RoR/odot/spec/spec_helper.rb:63:in <top (required)>': uninitialized constant Shoulda (NameError) from /home/dcdevman/Documents/devlang/RoR/odot/spec/models/todo_list_spec.rb:1:inrequire' from /home/dcdevman/Documents/devlang/RoR/odot/spec/models/todo_list_spec.rb:1:in <top (required)>' from /usr/lib/ruby/gems/2.2.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:inload' from /usr/lib/ruby/gems/2.2.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in block in load_spec_files' from /usr/lib/ruby/gems/2.2.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:ineach' from /usr/lib/ruby/gems/2.2.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in load_spec_files' from /usr/lib/ruby/gems/2.2.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:18:inrun' from /usr/lib/ruby/gems/2.2.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:103:in run' from /usr/lib/ruby/gems/2.2.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:17:inblock in autorun'

and no results in terms of whether the tests have passed or failed.

Seth Reece
Seth Reece
32,867 Points

Did you put it at the very end, outside of the Rspec.configure do block? Also, If you may have happened to use rspec 3.0 or better, there will be a spec_helper.rb and a rails_helper.rb. In this case all configuration goes in the rails_helper.rb file.