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

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

我现在正在使用 Rails 4.2。

我的测试文件如下所示:

# 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

在controller中,我设置了一个cookie为:cookies[:my_cookie] = 'some_value',所以在spec文件中,我会像上面一样访问特殊路径后测试cookie是否已经保存,但是失败了。 response只在controller中使用,但是这种情况下如何测试cookies呢?

参考:

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

这个gem可以做到:

show_me_the_cookies