Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec test if a session variable was set in a controller action

I have an rspec test that posts to an action. In the action, a session variable is definitely set, however in my controller test, the session hash is empty. How do I check in an Rspec test to see if the controller set the session variable to the right value?

like image 676
chris Avatar asked May 01 '13 21:05

chris


2 Answers

You can access session directly in rspec like this: session[:key]. Just compare that with the value you want.

like image 176
Billy Chan Avatar answered Sep 27 '22 17:09

Billy Chan


See this. The main problem is that the variable session is not accessible from the test code. To solve it, add the fowling lines at spec/spec_helper.rb:

def session
  last_request.env['rack.session']
end
like image 40
Eduardo Santana Avatar answered Sep 27 '22 15:09

Eduardo Santana