Setting sessions in controller testing

163 views
Skip to first unread message

bertly_the_coder

unread,
Dec 4, 2014, 1:52:03 AM12/4/14
to rubyonra...@googlegroups.com
Hi Guys,

I'm writing some controller tests in rspec and one of them needs a session to be set. Does anyone know how to set a session in rails tests?

BalaRaju Vankala

unread,
Dec 4, 2014, 5:32:02 AM12/4/14
to rubyonra...@googlegroups.com
Hello muchira, 

Are you using devise gem for login ?

On Thu, Dec 4, 2014 at 12:22 PM, bertly_the_coder <muc...@gmail.com> wrote:
Hi Guys,

I'm writing some controller tests in rspec and one of them needs a session to be set. Does anyone know how to set a session in rails tests?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a4281a9d-e8ae-46f2-9d0f-ee2f04109eb6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
----------------------------------------------------------------------------------------------------
Thank You.

Best Wishes,
 
BalaRaju Vankala,
8886565300.


Jason Fleetwood-Boldt

unread,
Dec 4, 2014, 9:30:36 AM12/4/14
to rubyonra...@googlegroups.com

If it is a normal controller or non-Webkit test, you can access the controller variable directly (I think with @controller), so you can just set session vars directly like:

@controller.session[:current_user] = @my_user

If you are writing a capybara webkit test, you don't have direct access to the same ruby environment where the session runs, but you can use this excellent gem to access it:



If you're using devise, there's patterns for doing this both in and out of webkit. Checkout this for an explanation: https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara


On Dec 4, 2014, at 1:52 AM, bertly_the_coder <muc...@gmail.com> wrote:

Hi Guys,

I'm writing some controller tests in rspec and one of them needs a session to be set. Does anyone know how to set a session in rails tests?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a4281a9d-e8ae-46f2-9d0f-ee2f04109eb6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

----

Jason Fleetwood-Boldt

All material © Jason Fleetwood-Boldt 2014. Public conversations may be turned into blog posts (original poster information will be made anonymous). Email ja...@datatravels.com with questions/concerns about this.

Reply all
Reply to author
Forward
0 new messages