rspec集成测试中的会话对象 [英] session object in rspec integration test

查看:86
本文介绍了rspec集成测试中的会话对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rspec和capybara进行集成测试。

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 %>

在我的请求说明中

session[:role] = "Role"
visit my_path

但是它抛出了一个错误 nil:NilClass的未定义方法'session'。

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.

推荐答案

您应该执行请求规范的方式是访问用户通常需要访问的任何URL,以便设置该会话变量。

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.

我不知道除了手动设置会话Cookie以外,其他任何方式都可以。 Capybara.current_session.driver.browser.set_cookie ,您可以执行此操作,但必须手动创建cookie值。

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.

这篇关于rspec集成测试中的会话对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆