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

I am getting the following rspec test fail when I run rspec spec and am complete lost. Can any one help?

I am trying to get the create, edit and destroy tests to work when following the todo list exercises but hit a wall. When I ran the rspec spec command to run all tests I get the following fail message and I can't work out how to solve this fail so can anyone help?

1) TodoListsController GET index assigns all todo_lists as @todo_lists
     Failure/Error: assigns(:todo_lists).should eq([todo_list])

       expected: [#<TodoList id: 2, title: "MyString", description: "My Description", created_at: "2014-08-13 21:22:23", updated_at: "2014-08-13 21:22:23">]
            got: #<ActiveRecord::Relation [#<TodoList id: 1, title: "Groceries", description: "Grocery list.", created_at: "2014-08-10 19:23:13", updated_at: "2014-08-10 19:23:13">, #<TodoList id: 2, title: "MyString", description: "My Description", created_at: "2014-08-13 21:22:23", updated_at: "2014-08-13 21:22:23">]>

       (compared using ==)

       Diff:
       @@ -1,2 +1,3 @@
       -[#<TodoList id: 2, title: "MyString", description: "My Description", created_at: "2014-08-13 21:22:23", updated_at: "2014-08-13 21:22:23">]
       +[#<TodoList id: 1, title: "Groceries", description: "Grocery list.", created_at: "2014-08-10 19:23:13", updated_at: "2014-08-10 19:23:13">,
       + #<TodoList id: 2, title: "MyString", description: "My Description", created_at: "2014-08-13 21:22:23", updated_at: "2014-08-13 21:22:23">]

     # ./spec/controllers/todo_lists_controller_spec.rb:37:in `block (3 levels) in <top (required)>'

2 Answers

David Gross
David Gross
19,443 Points
  context "logged in" do¬
      it "assigns all todo_lists as @todo_lists" do¬
         todo_list = user.todo_lists.create! valid_attributes¬
          get :index, {}, valid_session¬
          assigns(:todo_lists).should eq([todo_list])¬
          expect(assigns(:todo_lists).map(&:user)).to eq([user])¬
       end¬

Whats happening is that you are testing your users index page to see how many todo_lists you have. In your test you are only expecting one todolist but you are getting multiple todo_lists.

  expect(assigns(:todo_lists).map(&:user)).to eq([user])¬

The map method takes an enumerable object and a block, and runs the block for each element, outputting each returned value from the block. The & sign next to your symbol (&:user) calls the to_proc method. It is the same as writing x {x.user}. So what this code allows you to do is map through the users todo_list and and output it to your screen. That way you can pick up both of your todo lists.

Nathan F.
Nathan F.
30,773 Points

Users experiencing this issue might also be suffering from issues with the test database--some objects were persisting in the test database after the tests ran, even though it should be cleared. I was getting this exact failure, and numerous others, because the controller spec only expected the item it created, but got back several more items, and my tests were written to expect a specific count before and after methods were run, which were failing because the test database wasn't cleared.

I ended up following the wisdom of some Stack Overflow discussions and blog posts and added the database_cleaner gem to my testing group in my Gemfile, and changed the configuration to ensure that the database is truncated and cleaned before and after running tests. This resolved the failures.