samedi 12 septembre 2015

RSPEC Error: Expected #

I am trying to test the following controller action(Using Ruby on Rails, RSPEC, and FactoryGirl):

class ContactsController < ApplicationController

  before_action :authenticate_user!

  def index
    @contacts = current_user.contacts
    # @contacts = Contact.all
  end

Here is my contacts_controller_spec.rb file:

require 'rails_helper'

describe ContactsController do 

  before do 
    @user = FactoryGirl.create(:user_with_contacts)
    sign_in @user
  end

  describe "GET INDEX" do 
    it "assigns @contacts" do 
      expect(assigns(:contacts)).to eq(@user.contacts)
    end
  end

Failure/Error: expect(assigns(:contact)).to eq([contact])

       expected: [#<Contact id: 295, first_name: "Loy", email: "leon@hegmannhintz.net", phone_number: "6044339393", created_at: "2015-09-12 19:13:42", updated_at: "2015-09-12 19:13:42", last_name: "Wyman", user_id: 343>]
            got: #<Contact id: nil, first_name: nil, email: nil, phone_number: nil, created_at: nil, updated_at: nil, last_name: nil, user_id: nil>

And here is my users_spec.rb file:

FactoryGirl.define do
  factory :user do 
    email   { Faker::Internet.email }
    password { "32423fdsfasf42" }

    factory :user_with_contacts do 
      transient do 
        contacts_count 2
      end
      after(:create) do |user, evaluator|
        create_list(:contact, evaluator.contacts_count, user: user)
      end
    end
  end
end

Any Help please? I have been stuck on this for a long time.

If i call

puts @user.inspect

I get

#<User id: 340, email: "johnson_kaulke@brekke.com", encrypted_password: "$2a$04$Si5k6Q1eYERvhQITXKBoIOGEzPyK50E3IQ.yjRcqmDj...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2015-09-12 19:13:42", updated_at: "2015-09-12 19:13:42">

and

calling

  puts @user.contacts.inspect

I get

#<ActiveRecord::Associations::CollectionProxy [#<Contact id: 289, first_name: "Fae", email: "ariane@johnston.net", phone_number: "6044339393", created_at: "2015-09-12 19:13:42", updated_at: "2015-09-12 19:13:42", last_name: "Spinka", user_id: 340>, #<Contact id: 290, first_name: "Marcellus", email: "chloe_deckow@buckridge.net", phone_number: "6044339393", created_at: "2015-09-12 19:13:42", updated_at: "2015-09-12 19:13:42", last_name: "Bashirian", user_id: 340>]>

Its just when i call the assigns(:contacts) that the problem happens!

Aucun commentaire:

Enregistrer un commentaire