加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:07:04 所属栏目:百科 来源:网络整理
导读:如何测试使用Cookie的给定控制器操作? 功能测试中如何设置Cookie以及如何获取Cookie? 解决方法 这是适用于Rails 2.3.8的代码(行注释使测试不通过): test 'cookie testing should work' do @request.cookies['foo'] = 'Foo' # cookies['foo'] = 'Foo' # th
如何测试使用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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读