session object in rspec integration test

爱⌒轻易说出口 提交于 2019-12-05 21:04:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!