无法在 Rails 5 控制器测试中设置会话哈希 [英] Unable to set session hash in Rails 5 controller test
问题描述
根据 Rails Edge 指南所有 ActionDispatch::IntegrationTest HTTP 请求采用可选的命名关键字参数:
get post_url, params: { id: 12 }, session: { user_id: 5 }
太好了.现在,我在控制器测试中有以下代码:
test '应该从登录页面重定向如果用户登录' doget '/login', session: { user_id: users(:stephen).id }assert_redirected_to root_url, '预期重定向到 root'结尾
当我运行它时,我的测试失败,我看到以下弃用警告:
ActionDispatch::IntegrationTest HTTP 请求方法将只接受未来 Rails 版本中的以下关键字参数:参数、标题、环境、xhr
很明显,rails 不允许我传递名为 session 的关键字参数.
此外,在功能测试中设置会话的两种旧方法也不再有效:
测试某事"做session[:user_id] = users(:stephen).id# 等等结尾
<块引用>
NoMethodError: undefined method `session' for nil:NilClass
这也失败了:
测试某事"做get '/login', nil, nil, { user_id: users(:stephen).id }# 等等结尾
会话哈希被忽略,出现关于 rails 只接受 4 个不同命名参数的弃用警告.
是否还有其他人在使用 Rails 5.rc1 时遇到这种问题?
在 Rails 5 中,不再可能在控制器测试中访问会话:http://blog.napcs.com/2016/07/03/upgrading-rails-5-controller-tests/.建议是访问可以为您设置适当会话值的控制器方法.此评论显示了如何解决此限制的示例:https://github.com/rails/rails/issues/23386#issuecomment-192954569
# 辅助方法def sign_in_as(name)post login_url, params: { sig: users(name).perishable_signature )结尾类 SomeControllerTest设置 { sign_in_as '大卫' }测试真相"做..
According to the Rails Edge Guide all ActionDispatch::IntegrationTest HTTP requests take optional named keyword arguments:
get post_url, params: { id: 12 }, session: { user_id: 5 }
Great. Now, I've got the following code in a controller test:
test 'should redirect from login page if user is logged in' do
get '/login', session: { user_id: users(:stephen).id }
assert_redirected_to root_url, 'Expected redirect to root'
end
And when I run it, my test fails and I see the following deprecation warning:
ActionDispatch::IntegrationTest HTTP request methods will accept only
the following keyword arguments in future Rails versions:
params, headers, env, xhr
So obviously it's rails is not letting me pass a keyword argument named session.
Furthermore, both of the old methods of setting the session in a functional test no longer work either:
test "some thing" do
session[:user_id] = users(:stephen).id
# etc
end
NoMethodError: undefined method `session' for nil:NilClass
And this fails too:
test "some thing" do
get '/login', nil, nil, { user_id: users(:stephen).id }
# etc
end
The session hash is just ignored and the deprecation warning about rails only accepting 4 different named arguments appears.
Is anyone else having this sort of trouble with Rails 5.rc1?
In Rails 5 it is no longer possible to access session in controller tests: http://blog.napcs.com/2016/07/03/upgrading-rails-5-controller-tests/. The suggestion is to access the controller method that would set the appropriate session value for you. This comment shows and example of how you might work around this limitation: https://github.com/rails/rails/issues/23386#issuecomment-192954569
# helper method
def sign_in_as(name)
post login_url, params: { sig: users(name).perishable_signature )
end
class SomeControllerTest
setup { sign_in_as 'david' }
test 'the truth' do
..
这篇关于无法在 Rails 5 控制器测试中设置会话哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!