0
votes

I have a controller which makes a test:

@first_login = (@user.sign_in_count == 1)

And I need to set it up with devise in the rspec spec:

I have tried the four ideas: In all of them user is created like: user = Factory(:member) [see below for factory]

  user.stubs(:sign_in_count).returns(1)
  sign_in user
  get 'index'

  user.sign_in_count = 1 
  sign_in user # here user.sign_in_count == 1
  get 'index'  # here user.sign_in_count == 1

  sign_in user
  sign_in user
  get 'index'

  sign_in user
  user.sign_in_count = 1 
  get 'index'

But every time in the controller, the devise specific user properties are zeroed/nil-ed out.

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

Any ideas why?

[specs/factories.rb - factory_girl]

Factory.define :member, :class => User do |c|
  c.password "a123456h"
  c.first_name "Jack"
  c.last_name "Lee"
  c.email "[email protected]"
  c.address_street "123 Main"
  c.address_city "Anytown"
  c.address_state "NV"
  c.address_zip "40455"
  c.address_country "USA"
  c.when_account_created 2.days.ago
  c.change_password_count 3
  c.forgot_password_count 2
  c.created_at 2.days.ago
  c.updated_at 10.hours.ago
end
2
How are you creating your user instance? Are you using fixtures or factory girl?Matheus Moreira
I've created the instance both with factory_girl, as well as by hand. What happens is the controller seems to get a different user than we set up.jawspeak

2 Answers

0
votes

How/where is the user begin created in the spec?

0
votes

Save the user.

Seems like I have solved the problem with this in the spec:

  user = Factory(:customer)
  sign_in user
  user.sign_in_count = 1 
  user.save!
  get 'index'

Not sure if this is intended in the way devise/warden's current_user helper method is supposed to be used in the controller, but it works.

I'll leave it open if someone else has a better explanation.