Devise: session variable is empty on session_serializer.rb#store

I’ve been trying to test a very simple action on a controller with
this setup:

rspec 1.3.0
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Rails 2.3.8
devise 1.0.8

And this is the test:

before :each do
  @member = Factory.create(:member)
  sign_in @member
  @person = Person.new
end

it "should render form for a new person on GET people#new" do
  current_member.should_receive(:build_person).and_return(@person)

  get :new

  assigns[:person].should == @person
  response.should be_success
  response.should render_template("new")
end

This test creates a user, which is stored in the database with a
correct password. Running this test results in this error:

NoMethodError in ‘PeopleController Methods should render form for a
new person on GET people#new’
You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]=
/Library/Ruby/Gems/1.8/gems/warden-0.10.4/lib/warden/
session_serializer.rb:25:in store' /Library/Ruby/Gems/1.8/gems/devise-1.0.8/lib/devise/test_helpers.rb: 73:in sign_in’
/Users/damselem/Documents/project/spec/controllers/
people_controller_spec.rb:15:

Finished in 0.625754 seconds

23 examples, 1 failure

I wen to /Library/Ruby/Gems/1.8/gems/warden-0.10.4/lib/warden/
session_serializer.rb and I changed the file in order to print out
some values:

def store(user, scope)
return unless user
p scope
p key_for(scope)
p user
p serialize(user)
p session
session[key_for(scope)] = serialize(user)
end

When running the test again I get:

:member

“warden.user.member.key”

Member id: 1, email: “member2@hv_club.com”, encrypted_password:
“c79e5e4790643b284002bf6ad8045f53ff900afc”, password_salt:
“Rim9LiyKARU1SHQCWIb0”, confirmation_token: nil, confirmed_at: nil,
confirmation_sent_at: nil, reset_password_token: nil, remember_token:
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: “2010-07-09 16:45:44”, updated_at: “2010-07-09
16:45:44”

[Member(id: integer, email: string, encrypted_password: string,
password_salt: string, confirmation_token: string, confirmed_at:
datetime, confirmation_sent_at: datetime, reset_password_token:
string, remember_token: string, remember_created_at: datetime,
sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at:
datetime, current_sign_in_ip: string, last_sign_in_ip: string,
created_at: datetime, updated_at: datetime), 1]

nil

So it seems session is not initialized. Any ideas on how to fix this?
BTW, I already contacted Jose Valim through GitHub and he recommended
me to post my question in this group.

Thanks.

I’ve been doing some research on this problem, and I found this
discussion on Lighthouse:
https://rspec.lighthouseapp.com/projects/5645/tickets/963-request-is-nil

As it seems, this is not a problem with Rails 3 anymore, but how can I
make it work for rails 2.3.8?

On Jul 9, 2:45 pm, Daniel Salmeron A. [email protected]

On Jul 10, 2010, at 1:26 PM, Daniel Salmeron A. wrote:

And this is the test:
get :new
NoMethodError in 'PeopleController Methods should render form for a

  p scope

So it seems session is not initialized. Any ideas on how to fix this?
BTW, I already contacted Jose Valim through GitHub and he recommended
me to post my question in this group.session variable is empty on session_serializer.rb#store · Issue #374 · heartcombo/devise · GitHub

Thanks.

I’ve been doing some research on this problem, and I found this
discussion on Lighthouse: Lighthouse - Beautifully Simple Issue Tracking

As it seems, this is not a problem with Rails 3 anymore, but how can I
make it work for rails 2.3.8?

We’ve got this in an app using devise with rails 2.3.5 - should work
with 2.3.8.

before do
  request.env['warden'].stub(:authenticate!) { double(User) }
end

HTH,
David

On Jul 10, 2010, at 2:45 PM, Daniel Salmeron A. wrote:

I’ve been trying to test a very simple action on a controller with
@member = Factory.create(:member)
response.should be_success
You might have expected an instance of ActiveRecord::Base.
23 examples, 1 failure
p serialize(user)
Member id: 1, email: “member2@hv_club.com”, encrypted_password:
datetime, confirmation_sent_at: datetime, reset_password_token:

  request.env['warden'].stub(:authenticate!) { double(User) }
end

HTH,
David
Thanks David, but could you explain me how should I use this before do
block in order to sign in an user?

That’s all that’s in the before. It effectively signs in the user. If
you have a specific user you want to use, you can do

user = Factory(:user)
request.env[‘warden’].stub(:authenticate!) { user }

Thanks David, but could you explain me how should I use this before do
block in order to sign in an user? I am not a very experienced Rails
developer and I tried different things like:

13 before :each do
14 # @member = Factory.create(:member)
15 request.env[‘warden’].stub(:authenticate!) { double(Member) }
16 sign_in @member
17 @person = Person.new
18 end

which throws this error:

RuntimeError in ‘PeopleController Methods should render form for a new
person on GET people#new’
Could not find a valid mapping for
/Library/Ruby/Gems/1.8/gems/devise-1.0.8/lib/devise/mapping.rb:49:in
find_scope!' /Library/Ruby/Gems/1.8/gems/devise-1.0.8/lib/devise/test_helpers.rb: 71:insign_in’
/Users/damselem/Documents/Harvard/WebDev/hv_club/spec/controllers/
people_controller_spec.rb:16:

Finished in 0.437813 seconds

23 examples, 1 failure

Thanks David, and sorry for asking too much about this issue.

On Jul 10, 2010, at 3:21 PM, Daniel Salmeron A. wrote:

rspec 1.3.0
end

session_serializer.rb:25:in `store’
session_serializer.rb and I changed the file in order to print out
end
confirmation_sent_at: nil, reset_password_token: nil, remember_token:
datetime, current_sign_in_ip: string, last_sign_in_ip: string,
discussion on Lighthouse:Lighthouse - Beautifully Simple Issue Tracking
HTH,
David
Thanks David, but could you explain me how should I use this before do
block in order to sign in an user?

That’s all that’s in the before. It effectively signs in the user. If you have a specific user you want to use, you can do

user = Factory(:user)
request.env[‘warden’].stub(:authenticate!) { user }

Oh, I tried that too, but then current_member doesn’t work…

NameError in ‘PeopleController Methods should render form for a new
person on GET people#new’
undefined local variable or method `current_member’ for
#Spec::Rails::Example::ControllerExampleGroup::Subclass_2::Subclass_2:0x105b57ce0

This error is on the example itself, not the application code. Is there
a reference to current_member in the spec itself?

GitHub - heartcombo/devise at v1.0.8 . Any idea?

Oh, I tried that too, but then current_member doesn’t work…

NameError in ‘PeopleController Methods should render form for a new
person on GET people#new’
undefined local variable or method current_member' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_2::Subclass_2:0x105b57ce0> /Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/ test_process.rb:511:in method_missing’
/Users/damselem/Documents/Harvard/WebDev/hv_club/spec/controllers/
people_controller_spec.rb:21:

Finished in 0.543299 seconds

23 examples, 1 failure

which it seems to be available when using devise:
GitHub - heartcombo/devise at v1.0.8 . Any idea?

You’re right David, my fault. Than you very much for all your help.

Yep. I just changed current_member by @member, and it works now. Well,
it’s now asking me to create the “new” method which is what I expected
to receive. Thanks,

On Jul 10, 2010, at 3:38 PM, Daniel Salmeron A. wrote:

You’re right David, my fault. Than you very much for all your help.

You got it working then?