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?
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?
Hello muchira,
Are you using devise gem for login ?
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:
https://github.com/railsware/rack_session_access
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