In Rails integration tests, how do I set a session variable (:user_id in my case)?
在Rails集成测试中,如何设置会话变量(在我的情况下为:user_id)?
Obviously it's not a full integration test then, but given that in my app user authentication cannot happen without manual user interaction, can i somehow work around and have a session variable set manually?
显然,它不是一个完整的集成测试,但鉴于在我的应用程序中用户身份验证不能在没有手动用户交互的情况下发生,我可以以某种方式解决并手动设置会话变量吗?
Have tried the following: "session" is not available in that scope, open_session returns a session which I did not find a way to update.
尝试了以下内容:“session”在该范围内不可用,open_session返回一个我没有找到更新方法的会话。
2 个解决方案
#1
5
As far as I can tell, you can't. The session value in integration tests (in rails 3.2.2) is a copy. (Rails gods: Please correct me if I'm wrong.) You can set the value, but it won't affect the next get/post because it's a local copy of the real session. Great for assert tests, not so great for keeping state in session during the integration test.
据我所知,你做不到。集成测试中的会话值(在rails 3.2.2中)是一个副本。 (Rails gods:如果我错了,请纠正我。)你可以设置值,但它不会影响下一个get / post,因为它是真实会话的本地副本。非常适合断言测试,在集成测试期间保持状态不是很好。
I have a test controller to support my JavaScript testing via Evergreen.
我有一个测试控制器来支持我通过Evergreen进行的JavaScript测试。
My solution was to add a method to my test controller and a route available when I'm NOT in production, that allows me to set session values.
我的解决方案是在我的测试控制器中添加一个方法,并在我不在生产时提供一条可用的路由,这允许我设置会话值。
Basically my app_test#set_session method screens the parameter names for a prefix, if the prefix is found, then the value of session for the prefix stripped parameter name is set. Something like:
基本上我的app_test #set_session方法会屏蔽前缀的参数名称,如果找到前缀,则设置前缀剥离参数名称的会话值。就像是:
params.each() do |key, value|
next if ( key !~ /^set_session_/)
session[ key.slice( ("set_session_".size())..-1).to_sym ] = value
end
# don't forget to render something/anything
So, to set session variables during my integration tests, I have a small chunk of code like:
因此,要在集成测试期间设置会话变量,我有一小段代码,如:
prefix = "set_session_"
post( "/set_session.html", { (prefix + "user_id") => "My Name Is Mudd" } )
Not as pretty as "session[ :user_id ] = 'My Name Is Mudd'", but it works. My next post/get will see session[ :user_id ] set with the desired value.
不像“session [:user_id] ='我的名字是Mudd'”那么漂亮,但它确实有效。我的下一篇文章/获取将看到会话[:user_id]设置为所需的值。
This is easily extended to pass integers, symbols, etc, and of course, multiple session key/value pairs can be set at once. Not pretty, but not bad.
这很容易扩展为传递整数,符号等,当然,可以一次设置多个会话键/值对。不漂亮,但不坏。
#2
1
Why can authentication not happen without manual interaction? Does it use a CAPTCHA? If so, stub that part out for running in a test environment.
为什么没有手动交互不能进行身份验证?它是否使用CAPTCHA?如果是这样,那么存根就会在测试环境中运行。
#1
5
As far as I can tell, you can't. The session value in integration tests (in rails 3.2.2) is a copy. (Rails gods: Please correct me if I'm wrong.) You can set the value, but it won't affect the next get/post because it's a local copy of the real session. Great for assert tests, not so great for keeping state in session during the integration test.
据我所知,你做不到。集成测试中的会话值(在rails 3.2.2中)是一个副本。 (Rails gods:如果我错了,请纠正我。)你可以设置值,但它不会影响下一个get / post,因为它是真实会话的本地副本。非常适合断言测试,在集成测试期间保持状态不是很好。
I have a test controller to support my JavaScript testing via Evergreen.
我有一个测试控制器来支持我通过Evergreen进行的JavaScript测试。
My solution was to add a method to my test controller and a route available when I'm NOT in production, that allows me to set session values.
我的解决方案是在我的测试控制器中添加一个方法,并在我不在生产时提供一条可用的路由,这允许我设置会话值。
Basically my app_test#set_session method screens the parameter names for a prefix, if the prefix is found, then the value of session for the prefix stripped parameter name is set. Something like:
基本上我的app_test #set_session方法会屏蔽前缀的参数名称,如果找到前缀,则设置前缀剥离参数名称的会话值。就像是:
params.each() do |key, value|
next if ( key !~ /^set_session_/)
session[ key.slice( ("set_session_".size())..-1).to_sym ] = value
end
# don't forget to render something/anything
So, to set session variables during my integration tests, I have a small chunk of code like:
因此,要在集成测试期间设置会话变量,我有一小段代码,如:
prefix = "set_session_"
post( "/set_session.html", { (prefix + "user_id") => "My Name Is Mudd" } )
Not as pretty as "session[ :user_id ] = 'My Name Is Mudd'", but it works. My next post/get will see session[ :user_id ] set with the desired value.
不像“session [:user_id] ='我的名字是Mudd'”那么漂亮,但它确实有效。我的下一篇文章/获取将看到会话[:user_id]设置为所需的值。
This is easily extended to pass integers, symbols, etc, and of course, multiple session key/value pairs can be set at once. Not pretty, but not bad.
这很容易扩展为传递整数,符号等,当然,可以一次设置多个会话键/值对。不漂亮,但不坏。
#2
1
Why can authentication not happen without manual interaction? Does it use a CAPTCHA? If so, stub that part out for running in a test environment.
为什么没有手动交互不能进行身份验证?它是否使用CAPTCHA?如果是这样,那么存根就会在测试环境中运行。