rspec 集成测试中的会话对象
我正在使用 rspec 和 capybara 进行集成测试。
他们是在请求规范中创建会话对象的一种方法吗?
我有一个视图页面,在其中使用会话对象来检查其值以显示选择性内容。
我面临的问题是我无法在请求规范中创建会话对象。 这是视图的示例:
<% if session[:role] == "Role" %>
---content---
<% else %>
--content--
<% end %>
在我的请求规范中
session[:role] = "Role"
visit my_path
,但它向我抛出一个错误“nil:NilClass 的未定义方法‘session’”。
我还尝试研究创建水豚的会话对象。但找不到任何东西。
他们有解决办法吗?我找不到与此相关的任何内容,似乎这是不可能的。非常感谢您的一点帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该执行请求规范的方式是访问用户通常需要访问的任何 URL,以便设置该会话变量。
除了手动设置会话 cookie 之外,我不知道还有什么其他方法。
Capybara.current_session.driver.browser.set_cookie
让您可以执行此操作,但您必须手动创建 cookie 值。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.