根据
Rails Edge Guide,所有ActionDispatch :: IntegrationTest HTTP请求都采用可选的命名关键字参数:
get post_url,params:{id:12},session:{user_id:5}
大.现在,我在控制器测试中得到以下代码:
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
当我运行它时,我的测试失败,我看到以下弃用警告:
ActionDispatch::IntegrationTest HTTP request methods will accept only the following keyword arguments in future Rails versions: params,headers,env,xhr
所以显然它的rails不会让我传递一个名为session的关键字参数.
test "some thing" do session[:user_id] = users(:stephen).id # etc end
NoMethodError: undefined method `session’ for nil:NilClass
这也失败了:
test "some thing" do get '/login',nil,{ user_id: users(:stephen).id } # etc end
只会忽略会话哈希,并且会显示有关仅接受4个不同命名参数的rails的弃用警告.
是否有其他人在使用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
# 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 ..