ruby-on-rails – 如何在Rails的功能测试中测试Cookie状态?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何在Rails的功能测试中测试Cookie状态?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何测试使用Cookie的给定控制器操作?

功能测试中如何设置Cookie以及如何获取Cookie?

解决方法

这是适用于Rails 2.3.8的代码(行注释使测试不通过):
test 'cookie testing should work' do
  @request.cookies['foo'] = 'Foo'
  # cookies['foo'] = 'Foo'
  # this does not work to: CGI::Cookie.new('foo','bar')
  get :index # does: cookies[:foo] = (cookies['foo'] || "") + " bar!" 
  # the cookie key in the controller can by a symbol,but not in the test
  assert_response :success
  assert_not_nil cookies['foo'],"Cookie with foo key should not be nil. Debug: Cookies=#{cookies.inspect}"
  assert_equal "Foo bar!",cookies['foo'],"Debug: Cookies=#{cookies.inspect}"
  # assert_not_nil @cookies['foo'],"Cookie with foo key should not be nil. Debug: Cookies=#{@cookies.inspect}"
  # assert_not_nil @request.cookies['foo'],"Cookie with foo key should not be nil. Debug: Cookies=#{@request.cookies.inspect}"
  # assert_equal "Foo bar!",@request.cookies['foo'],"Debug: Cookies=#{@request.cookies.inspect}"
end

我花了很长时间寻找答案.

猜你在找的Ruby相关文章