[rspec-users] Devise: session variable is empty on session_serializer.rb#store

David Chelimsky dchelimsky at gmail.com
Sat Jul 10 15:53:16 EDT 2010


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

> 
> On Jul 10, 2:31 pm, David Chelimsky <dchelim... at gmail.com> wrote:
>> On Jul 10, 2010, at 1:26 PM, Daniel Salmeron Amselem wrote:
>> 
>> 
>> 
>> 
>> 
>>> On Jul 9, 2:45 pm, Daniel Salmeron Amselem <daniel.amse... at gmail.com>
>>> wrote:
>>>> 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:
>> 
>>>> 1)
>>>> 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 at 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.http://github.com/plataformatec/devise/issues/issue/374
>> 
>>>> 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?
>> 
>> 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
> 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 }


> 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:in `sign_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.




More information about the rspec-users mailing list