无法在rspec控制器测试中设置Devise用户属性sign_in_count。它在控制器中复位为0

时间:2011-06-05 19:09:56

标签: ruby-on-rails rspec devise

我有一个控制器进行测试:

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

我需要在rspec规范中设计它:

我尝试过这四个想法:在所有这些想法中,用户的创建方式如下:user = Factory(:member) [见下面的工厂]

  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'

但是每次在控制器中,设备特定的用户属性都被清零/清零。

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

任何想法为什么?

[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 "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

2 个答案:

答案 0 :(得分:0)

如何/在哪里开始在规范中创建用户?

答案 1 :(得分:0)

保存用户。

好像我已经在规范中解决了这个问题:

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

不确定这是否打算采用devise / warden的current_user辅助方法应该在控制器中使用的方式,但是它可行。

如果其他人有更好的解释,我会保持开放。