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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With