3

This is very easy question but I cannot find any solution for this. It has been 3 days. Please help.

So I have this in

users_controller.rb

def index
  @users = @users.normal.order(:name).page params[:page]
end

So I have this in my users_controller_spec.rb

it "assign all normal users to @users" do
  users = User.normal.all
  get :index
  expect(assigns(:users)).to eq(users)
end

My code is based from https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs .

After running rspec in my terminal, this is the output.

  Diff:
   @@ -1,4 +1,4 @@
   -[#<User:0x00000004aa32b0
   +[#<User:0x00000004a085a8
      id: 2,
      name: "Christian Galamay",
      admin: false,
   @@ -20,7 +20,7 @@
      updated_at: Wed, 13 Apr 2016 01:46:17 UTC +00:00,
      role: "Normal",
      avatar: nil>,
   - #<User:0x00000004aa2ea0
   + #<User:0x000000049dbd78
      id: 3,
      name: "Grace Sojor",
      admin: false,
   @@ -42,7 +42,7 @@
      updated_at: Wed, 13 Apr 2016 01:46:17 UTC +00:00,
      role: "Normal",
      avatar: nil>,
   - #<User:0x00000004aa2ab8
   + #<User:0x000000049db760
      id: 4,
      name: "Karl Pandacan",
      admin: false,

The output says all the values are the same except with the hex after the User. So my question is (1) Why the hex after User is included in comparing the two ActiveRecord (2) Is there any method or function to use to ignore the hex in User. Thank You

3 Answers 3

3

you can simply check the array of users instead of active record relation

expect(assigns(:users).to_a).to eq(users.to_a)
Sign up to request clarification or add additional context in comments.

Comments

3

Your arrays are equivalent in value, however your spec is failing because you're comparing reference equality rather than value equality since your controller is loading users into a separate variable than your specs.

Try comparing the values of your collection using as_json:

expect(assigns(:users).as_json).to eq(users.as_json)

1 Comment

The explanation isn't correct: the eq matcher compares using == not equal?
2

The output is slightly misleading. The 2 things are not equal because they are relation objects and the == between two ActiveRecord::Relation objects works by comparing the generated sql. In your case the relations aren't equal: in the controller you are paginating and setting an order.

Rspec tries to be helpful by showing a diff of the to_s output of the 2 objects, however this ends up comparing the result of the query (which are indeed the same objects) and so the only difference it finds in that string output are the object ids. This is partly luck - with no order clause the objects could have been returned in a different order. To be explicit: the diff output shown is just there to help you, it doesn't mean that that is how rspec did the comparison.

If what you want is to compare the results of the query, then you can use to_a to do that:

 expect(assigns(:users)).to eq(users.to_a)

If you don't care about order you could also do

expect(assigns(:users)).to match_array(users)

This is a slightly different test: it won't fail if the db decides to return results in a different, which is possible as I mentioned above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.