问题
I am using rspec and capybara for integration testing.
Is their a way to make session objects in request specs?
I have a view page in which I use a session object to check its value to display selective content.
The problem am facing is that I cannot create a session object in request spec. Here is an example of the view:
<% if session[:role] == "Role" %>
---content---
<% else %>
--content--
<% end %>
And inside my request spec
session[:role] = "Role"
visit my_path
But it throws me an error "undefined method `session' for nil:NilClass".
I also tried looking into creating session objects of capybara.But couldnt find anything.
Is their any workaround for this? I can not find anything related to this, it seems that it is not possible.A little help is really appreciated.
回答1:
The way you are supposed to do request specs is visit whatever URL the user would normally need to visit in order for that session variable to be set.
I don't know of any other way, other perhaps than setting the session cookie by hand. Capybara.current_session.driver.browser.set_cookie
let's you do this but you would have to create the cookie value by hand.
来源:https://stackoverflow.com/questions/8726192/session-object-in-rspec-integration-test