UPDATED: I realise now that I’ve been misreading the diff, and I have a string or symbol on one side of the comparison. Still unsure how I should be putting the expectation in this test however..
I’m new to Rspec and TDD in general, and I’ve run into this problem. I have a controller that does this:
def index
@users = User.page(params[:page])
end
(I’m using Kaminara to paginate)
And a spec:
describe "when the user DOES have admin status" do
login_admin_user
it "should allow the user access to the complete user list page" do
get :index
response.response_code.should == 200
end
describe "and views the /users page" do
before(:each) do
User.stub(:page) {[ mock_model(User), mock_model(User), mock_model(User) ]}
end
it "should show all users" do
get :index
assigns (:users).should =~ User.page
end
end
end
The spec fails with the following:
Failure/Error: assigns (:users).should =~ User.page
expected: [#<User:0x5da86a8 @name="User_1004">, #<User:0x5d9c90c @name="User_1005">, #<User:0x5d93ef6 @name="User_1006">]
got: :users (using =~)
Diff:
@@ -1,4 +1,2 @@
-[#<User:0x5da86a8 @name="User_1004">,
- #<User:0x5d9c90c @name="User_1005">,
- #<User:0x5d93ef6 @name="User_1006">]
+:users
Those result sets look identical. Why does this spec fail? Thanks in advance!
I think the problem is the space after
assigns. It’s comparing the symbol:usersto your list. Change it to:And just a note on how to read Rspec failures. The part after
expected, is what you gave toshould, whereas the part aftergotis the value your code actually produced. So it’s clear from the report that the result sets were not identical.