marcamillion
marcamillion

Reputation: 33755

Why does my AR relation return empty in RSpec but returns objects in Rails Console?

This is my Spec:

  describe 'GET #index' do
    let(:family_tree) { create(:family_tree, user: @user) }
    let(:node1) { create(:node, family_tree: family_tree, user: @user) }
    let(:node2) { create(:node, family_tree: family_tree, user: @user) }

    before { login_user }

it "assigns all the nodes that are tagged with the current_user email to local variable 'tagged_nodes'" do      
  get :index            

  u1 = create(:user)
  u2 = create(:user)
  n1 = create(:node, family_tree: family_tree)      
  n1.user_tag_list.add(u1.email, u2.email)

  expect(assigns(:tagged_nodes)).to include(n1)
 end
end

That relates to my Dashboard#Index, where it is testing the assignment of @tagged_nodes per this line:

@tagged_nodes = nodes_that_are_tagged_with(current_user)

Here is the method that is doing the lookup, which I have stored in a Concern (which is properly included in the controller):

  def nodes_that_are_tagged_with(user)
    Node.includes(:user_tags).tagged_with(user.email)
  end

The error I am getting is this:

 1) DashboardController GET #index assigns all the nodes that are tagged with the current_user email to local variable 'tagged_nodes'
     Failure/Error: expect(assigns(:tagged_nodes)).to include(n1)
       expected #<ActiveRecord::Relation []> to include #<Node id: 21, name: "Miss Wendy McClure", family_tree_id: 38, user_id: nil, media_id: 21, media_type: "Video", created_at: "2015-05-07 22:56:09", updated_at: "2015-05-07 22:56:09", circa: "2015-05-25 00:00:00", is_comment: nil>
       Diff:
       @@ -1,2 +1,2 @@
       -[#<Node id: 21, name: "Miss Wendy McClure", family_tree_id: 38, user_id: nil, media_id: 21, media_type: "Video", created_at: "2015-05-07 22:56:09", updated_at: "2015-05-07 22:56:09", circa: "2015-05-25 00:00:00", is_comment: nil>]
       +[]

Notice that the relation being returned is empty.

When I try to run this Node.includes(:user_tags)... query at the command line, I get the proper results I would expect:

[17] pry(main)> n
=> #<Node id: 7, name: "Hercules", family_tree_id: 57, user_id: 57, media_id: 120, media_type: "Video", created_at: "2015-03-12 08:54:29", updated_at: "2015-03-31 21:48:05", circa: nil, is_comment: nil>
[18] pry(main)> n.user_tags
=> [#<ActsAsTaggableOn::Tag id: 4, name: "[email protected]", taggings_count: 2>, #<ActsAsTaggableOn::Tag id: 6, name: "[email protected]", taggings_count: 1>]
[19] pry(main)> u
=> #<User id: 52, email: "[email protected]", encrypted_password: "$2a$10$KaX1kvtIw1.jGITnt9Czqeq3xTzhY3OM052NSHsL5Lf...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 5, current_sign_in_at: "2015-04-03 17:10:28", last_sign_in_at: "2015-04-03 00:38:24", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-03-05 01:36:31", updated_at: "2015-04-03 17:10:28", first_name: "Gerry ", confirmation_token: nil, confirmed_at: "2015-03-05 01:36:52", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "uncle", avatar: nil, invitation_token: nil, invitation_created_at: "2015-03-05 01:36:31", invitation_sent_at: "2015-03-05 01:36:31", invitation_accepted_at: "2015-03-05 01:36:52", invitation_limit: nil, invited_by_id: 1, invited_by_type: "User", invitations_count: 0, bio: nil, last_name: "Atrick", gender: 0>
[20] pry(main)> u.email
=> "[email protected]"
[21] pry(main)> Node.includes(:user_tags).tagged_with(u.email)
  ActsAsTaggableOn::Tag Load (2.7ms)  SELECT "tags".* FROM "tags"  WHERE (LOWER(name) = LOWER('[email protected]'))
  Node Load (2.9ms)  SELECT "nodes".* FROM "nodes" JOIN taggings nodes_taggings_baebc90  ON nodes_taggings_baebc90.taggable_id = "nodes".id AND nodes_taggings_baebc90.taggable_type = 'Node' AND nodes_taggings_baebc90.tag_id = 4
  ActsAsTaggableOn::Tagging Load (2.5ms)  SELECT "taggings".* FROM "taggings"  WHERE "taggings"."context" = 'user_tags' AND "taggings"."taggable_type" = 'Node' AND "taggings"."taggable_id" IN (6, 7)
  ActsAsTaggableOn::Tag Load (1.0ms)  SELECT "tags".* FROM "tags"  WHERE "tags"."id" IN (4, 6)
=> [#<Node id: 6, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 118, media_type: "Video", created_at: "2015-03-09 20:57:19", updated_at: "2015-04-03 00:25:38", circa: nil, is_comment: nil>,
 #<Node id: 7, name: "Hercules", family_tree_id: 57, user_id: 57, media_id: 120, media_type: "Video", created_at: "2015-03-12 08:54:29", updated_at: "2015-03-31 21:48:05", circa: nil, is_comment: nil>]

For the life of me, I can't figure out why the spec is returning an empty relation.

Upvotes: 0

Views: 624

Answers (1)

infused
infused

Reputation: 24337

The problem is that you are creating the records after you've already hit the index action. Move get :index below the record creation:

it "assigns all the nodes that are tagged with the current_user email to local variable 'tagged_nodes'" do      
  u1 = create(:user)
  u2 = create(:user)
  n1 = create(:node, family_tree: family_tree)      
  n1.user_tag_list.add(u1.email, u2.email)

  get :index  

  expect(assigns(:tagged_nodes)).to include(n1)
end

Upvotes: 1

Related Questions