Take the 2-minute tour ×

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]

  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 "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
share|improve this question
How are you creating your user instance? Are you using fixtures or factory girl? –  Matheus Moreira Jun 5 '11 at 19:21
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 Jun 6 '11 at 19:53
add comment

2 Answers

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

share|improve this answer
I updated how the user is created: user = Factory(:member) with factory girl. And Hi Dave, we met at a Hashrocket Chicago opening party with Josh. –  JAWspeak Jun 6 '11 at 15:06
add comment
up vote 0 down vote accepted

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 
  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.

share|improve this answer
add comment

Your Answer


