Rails + Cucumber/Capybara: How to set/retrieve cookies in tests?

前端 未结 3 1776
你的背包
你的背包 2021-02-12 11:48

I\'m implementing a lazy login feature. My cucumber feature should describe it:

    Feature: User log in

        Scenario: Lazy login
            Given I didn\'         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-12 12:16

    I needed just to test the cookie values

    Inspiration taken from https://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles

    and ported to Rails 5.x

    Create features/support/cookies.rb

    With content

    module Capybara
      class Session
        def cookies
          @cookies ||= ActionDispatch::Request.new(Rails.application.env_config.deep_dup).cookie_jar
        end
      end
    end
    
    Before do
      allow_any_instance_of(ActionDispatch::Request).to receive(:cookie_jar).and_return(page.cookies)
      allow_any_instance_of(ActionDispatch::Request).to receive(:cookies).and_return(page.cookies)
    end
    

    Then the step for testing

    Then('is set cookie {string} with value {string}') do |cookie, value|
      expect(page.cookies.signed[cookie]).to eq value
    end
    

提交回复
热议问题