Can not set Devise user property sign_in_count in rspec controller test. It is reset to 0 in the controller
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.de开发者_开发百科fine :member, :class => User do |c|
c.password "a123456h"
c.first_name "Jack"
c.last_name "Lee"
c.email "jxxxx@example.com"
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
How/where is the user begin created in the spec?
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.
精彩评论