简体   繁体   English

如何在功能测试中使用Rspec和Capybara测试cookie?

[英]How to test cookies with Rspec and Capybara in a feature test?

I am using Rails 4.2 now. 我现在正在使用Rails 4.2。

My test file looks like: 我的测试文件如下所示:

# spec/features/my_spec.rb
require 'spec_helper'
feature 'MyFeature', type: :feature do
  describe 'MyPage' do
    it 'Saved the cookie' do
      visit my_path
      expect(response.cookies[:my_cookie]).to eq('some_value')
    end
  end
end

In the controller, I set a cookie as: cookies[:my_cookie] = 'some_value' , so in the spec file, I will test if the cookie has been saved after access the special path as above, but failed. 在控制器中,我将cookie设置为: cookies[:my_cookie] = 'some_value' ,因此在规范文件中,我将测试访问上述特殊路径后是否已保存cookie,但失败了。 The response is only used in the controller, but how to test cookies in this case? response仅在控制器中使用,但是在这种情况下如何测试cookie?

Reference: 参考:

https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/cookies https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/cookies

This gem can do it: 这个宝石可以做到:

show_me_the_cookies show_me_the_cookies

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM