Sessions with rspec and Rack.Test

3,242 views
Skip to first unread message

garren

unread,
Mar 31, 2010, 10:23:48 AM3/31/10
to sinatrarb
I have searched the net and read every message on this group to try
and figure out how to get sessions working with my tests. I cant seem
to get it to work. I have tried both methods of:

{ "rack.session" => { :username => 'garren'}}

or

:session => {:username => "garren"}

If and one could help me it would be greatly appreciated.
Here is the sample code I've been testing with

http://gist.github.com/350381

Ben Lovell

unread,
Mar 31, 2010, 11:17:36 AM3/31/10
to sina...@googlegroups.com
I'm seeing the same since upgrading to the 1.0 sinatra, previous version doesn't exhibit this behaviour. Pushing a value into the session works when you're checking the result in the same route, but doesn't survive redirects etc to other routes. I've pushed a quick example up here:


I think this is a bug.

Cheers,
Ben

Roland Swingler

unread,
Mar 31, 2010, 2:15:17 PM3/31/10
to sina...@googlegroups.com
Don't know if the info in this thread
http://groups.google.com/group/sinatrarb/browse_thread/thread/4f32eb1165712291
is still relevant to 1.0, but might help.

Roland

> --
> You received this message because you are subscribed to the Google Groups
> "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to
> sinatrarb+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sinatrarb?hl=en.
>

Ben Lovell

unread,
Mar 31, 2010, 2:45:55 PM3/31/10
to sina...@googlegroups.com
On 31 March 2010 19:15, Roland Swingler <roland....@gmail.com> wrote:
Don't know if the info in this thread
http://groups.google.com/group/sinatrarb/browse_thread/thread/4f32eb1165712291
is still relevant to 1.0, but might help.

Roland

On Wed, Mar 31, 2010 at 4:17 PM, Ben Lovell <benjami...@gmail.com> wrote:
> On 31 March 2010 15:23, garren <garren...@gmail.com> wrote:
>>
>> I have searched the net and read every message on this group to try
>> and figure out how to get sessions working with my tests. I cant seem
>> to get it to work. I have tried both methods of:
>>
>> { "rack.session" =>  { :username => 'garren'}}
>>
>> or
>>
>> :session => {:username => "garren"}
>>
>> If and one could help me it would be greatly appreciated.
>> Here is the sample code I've been testing with
>>
>> http://gist.github.com/350381
>>
>
> I'm seeing the same since upgrading to the 1.0 sinatra, previous version
> doesn't exhibit this behaviour. Pushing a value into the session works when
> you're checking the result in the same route, but doesn't survive redirects
> etc to other routes. I've pushed a quick example up here:
> http://github.com/benlovell/sinatra-sessions
> I think this is a bug.
> Cheers,
> Ben
>

As an aside to this and going slightly OT how do most people test their sinatra apps? From what I've seen it is mostly fairly course-grained with integration style tests. I have been trying to do it the rails way at a much finer grained level with specs asserting behaviour and a cucumber feature holding it all together. I'm starting to feel it is much more beneficial to test my sinatra apps at a higher level, rather than asserting on the minutia...

Ben

Damian Janowski

unread,
Apr 1, 2010, 10:16:06 AM4/1/10
to sina...@googlegroups.com

If you really want to do that then you have to use something like
this: http://gist.github.com/295801

Then use like:

get "/", {}, session: {user: 1}

Damian Janowski

unread,
Apr 1, 2010, 10:19:06 AM4/1/10
to sina...@googlegroups.com
On Wed, Mar 31, 2010 at 12:17 PM, Ben Lovell <benjami...@gmail.com> wrote:
> I'm seeing the same since upgrading to the 1.0 sinatra, previous version
> doesn't exhibit this behaviour.

That's because the old behavior was 'wrong' and (as a side effect)
didn't allow for integration tests.

> Pushing a value into the session works when
> you're checking the result in the same route, but doesn't survive redirects
> etc to other routes.

If you want your cookies to survive, you should try Webrat or Capybara
(I recently switched to Capybara because it's much more consistent in
its API, smaller, and provides out-of-the-box Selenium 2.0 support).

Ben Lovell

unread,
Apr 1, 2010, 11:34:56 AM4/1/10
to sina...@googlegroups.com
Pretty much as I suspected. This of course doesn't make it easy to spec' out on a finer-grained level as I alluded to in a previous post. As a side note I am actually finding it easier to adopt the opposite style of testing with sinatra i.e. a broad integration style test at the very top-level and don't sweat the details lower down. Not sure I'd want to go this route with rails but that's another story :)

Cheers Damian.

Ben 

garren

unread,
Apr 1, 2010, 3:48:17 PM4/1/10
to sinatrarb
So when people say integration tests what do they mean? I always
assumed an integration test was a full test from input to the database
and back? How do you achieve this without storing any cookies that you
would usually store?

On Apr 1, 5:34 pm, Ben Lovell <benjamin.lov...@gmail.com> wrote:
> On 1 April 2010 15:19, Damian Janowski <damian.janow...@gmail.com> wrote:
>
>
>
> > On Wed, Mar 31, 2010 at 12:17 PM, Ben Lovell <benjamin.lov...@gmail.com>

Damian Janowski

unread,
Apr 1, 2010, 4:58:22 PM4/1/10
to sina...@googlegroups.com
On Thu, Apr 1, 2010 at 4:48 PM, garren <garren...@gmail.com> wrote:
> So when people say integration tests what do they mean? I always
> assumed an integration test was a full test from input to the database
> and back? How do you achieve this without storing any cookies that you
> would usually store?

Try Webrat or Capybara.

The default Monk skeleton shows how to set up Webrat and Sinatra
(hurry up before we change it to Capybara).

Alex Chaffee

unread,
Apr 1, 2010, 7:04:31 PM4/1/10
to sina...@googlegroups.com
Rack::Test preserves the rack.session environment variable. So if you define

def session
  last_request.env['rack.session']
end

in your test/spec helpers then you'll get access to a hash. Set stuff in it before calling "get" or "post" and it'll be there for the app under test.

garren

unread,
Apr 2, 2010, 3:32:47 AM4/2/10
to sinatrarb
Thanks for the help, I'll try out both techniques.

@Damian, I'll try out Capybara... you seem to be recommending it so
highly.

On Apr 2, 1:04 am, Alex Chaffee <ale...@gmail.com> wrote:
> Rack::Test preserves the rack.session environment variable. So if you define
>
> def session
>   last_request.env['rack.session']
> end
>
> in your test/spec helpers then you'll get access to a hash. Set stuff in it
> before calling "get" or "post" and it'll be there for the app under test.
>
> ---

> Alex Chaffee - a...@cohuman.com -http://alexch.github.com

> On Wed, Mar 31, 2010 at 7:23 AM, garren <garren.sm...@gmail.com> wrote:
> > I have searched the net and read every message on this group to try
> > and figure out how to get sessions working with my tests. I cant seem
> > to get it to work. I have tried both methods of:
>
> > { "rack.session" =>  { :username => 'garren'}}
>
> > or
>
> > :session => {:username => "garren"}
>
> > If and one could help me it would be greatly appreciated.
> > Here is the sample code I've been testing with
>
> >http://gist.github.com/350381
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "sinatrarb" group.
> > To post to this group, send email to sina...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > sinatrarb+...@googlegroups.com<sinatrarb%2Bunsu...@googlegroups.com>

Message has been deleted

Alex Chaffee

unread,
Apr 22, 2010, 7:43:24 PM4/22/10
to sinatrarb
I just upgraded to Sinatra 1.0 and found that my previous technique
didn't work any more. I just spent several hours tunneling way too
deeply into Rack::Test and here's what I came up with:

http://gist.github.com/375973

(Thanks to Damian's gist for inspiring me to boldly pack and unpack
the session cookie...)

I think this should probably turn into a patch to Rack::Test that more
cleanly exposes some of the inner cookie-handling code, or makes an
effort to save and reload session data. This is a pretty common use
case for testing and it'd be nice if we could consistently access,
change, and preserve session data across multiple requests.

Note that it only works if you're using cookie sessions in Sinatra; if
you want to use a different type of sessions in your live app, you can
still use cookie sessions in your tests by doing something like

class LoginApp < Sinatra::Base
use Rack::Session::Cookie
end

- Alex


On Apr 2, 12:32 am, garren <garren.sm...@gmail.com> wrote:
> Thanks for the help, I'll try out both techniques.
>
> @Damian, I'll try out Capybara... you seem to be recommending it so
> highly.
>
> On Apr 2, 1:04 am,AlexChaffee <ale...@gmail.com> wrote:
>
>
>
> > Rack::Test preserves the rack.session environment variable. So if you define
>
> > def session
> >   last_request.env['rack.session']
> > end
>
> > in your test/spec helpers then you'll get access to a hash. Set stuff in it
> > before calling "get" or "post" and it'll be there for the app under test.
>
> > ---
> >AlexChaffee - a...@cohuman.com -http://alexch.github.com
> > Stalk me:http://friendfeed.com/alexch|http://twitter.com/alexch|http://alexch.tumblr.com
>
> > On Wed, Mar 31, 2010 at 7:23 AM, garren <garren.sm...@gmail.com> wrote:
> > > I have searched the net and read every message on this group to try
> > > and figure out how to getsessionsworking with my tests. I cant seem
> > > to get it to work. I have tried both methods of:
>
> > > { "rack.session" =>  { :username => 'garren'}}
>
> > > or
>
> > > :session => {:username => "garren"}
>
> > > If and one could help me it would be greatly appreciated.
> > > Here is the sample code I've been testing with
>
> > >http://gist.github.com/350381
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "sinatrarb" group.
> > > To post to this group, send email to sina...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > sinatrarb+...@googlegroups.com<sinatrarb%2Bunsubscribe@googlegroups .com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/sinatrarb?hl=en.

--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To post to this group, send email to sina...@googlegroups.com.
To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages