ruwhan
ruwhan

Reputation: 117

Rspec Test Always Failed for Class Method

as the subject said. It is seems the implemented code always create a new record instead of get existing one.

Here is my rspec test code:

describe "#from_facebook" do 
  it "return existing related user" do 
    existing_user = FactoryGirl.create(:user)
    user_from_facebook = User.from_facebook({ uid: existing_user.uid })
    expect(user_from_facebook.email).to eql existing_user.email
  end
end

and here is the test implementation:

def self.from_facebook(fb_auth)
where(provider: "facebook", uid: fb_auth[:uid]).first_or_create do |user|
    user.email = fb_auth[:email]
    user.password = Devise.friendly_token[0, 20]
  end
end

I always got this:

1) User#from_facebook return existing related user
 Failure/Error: expect(user_from_facebook.email).to eql existing_user.email

   expected: "[email protected]"
        got: nil

   (compared using eql?)
 # ./spec/models/user_spec.rb:48:in `block (3 levels) in <top (required)>'

And here are the console results via puts. for existing_user:

#<User id: 105, email: "[email protected]", encrypted_password: "$2a$04$90uzEdbwfnqfUL0CDhNOH.THUbzovFyJK1OPg7VcgxL...", 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-08-30 04:12:59", updated_at: "2015-08-30 04:12:59", auth_token: "vkvm2-fLe5x1dZGbewqw", provider: nil, uid: "133290925">

for user_from_facebook:

#<User id: nil, email: nil, encrypted_password: "$2a$04$lxdBWBsXY4febfgurgHJKOdNcOMCuQQO2DHshWChnxA...", 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: nil, updated_at: nil, auth_token: "", provider: "facebook", uid: "133290925">

Please let me know what is wrong here. Thanks in advance.

Upvotes: 1

Views: 88

Answers (2)

tessi
tessi

Reputation: 13574

Your User factory doesn't fully setup the user. It creates the following user object:

#<User id: 105,
       email: "[email protected]",
       encrypted_password: "$2a$04$90uzEdbwfnq...",
       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-08-30 04:12:59",
       updated_at: "2015-08-30 04:12:59",
       auth_token: "vkvm2-fLe5x1dZGbewqw",
       provider: nil,
       uid: "133290925"

Note that the provider is nil. Thus, your from_facebook method (which looks for the uuid and the provider) cannot find the user.

As a solution, use the following to create your existing_user in the spec:

existing_user = FactoryGirl.create(:user, provider: 'facebook')

Upvotes: 1

Josh
Josh

Reputation: 8596

Are you setting a uid in the FactoryGirl.create(:user)?

Can you post the content of your user factory? You might want to create a facebook_user factory that sets a unique uid

Upvotes: 0

Related Questions