Skip to content

cookies != response.cookies #2273

@ChrisCPO

Description

@ChrisCPO

Ruby version: 2.6.5p114
Rails version: 5.2.3
RSpec version: 3.8

When testing a controller that deletes a cookie.
The cookie is removed from the response but not the cookies object.
Is this possibly something we are doing wrong in our app?

pry in test after subject is called
[3] pry(#<RSpec::Matchers::DSL::Matcher>)> cookies
=> #<ActionDispatch::Cookies::CookieJar:0x00007ff11de14478
 @committed=false,
 @cookies={"foo"=>"encrypted_string"},
 @delete_cookies={},
 @request= <...omitted...>
[4] pry(#<RSpec::Matchers::DSL::Matcher>)> response.cookies
=> {"foo"=>nil}

Expected behaviour

cookies == request.cookies

Can you provide an example app?

class FoosController < ApplicationController
  def show
    cookies.delete(:foo)

    render plain: "foo"
  end
end

describe FooController do
  describe "#show" do
    subject { -> { get :show } }

    context "when a foo cookie is present" do
      before { cookies[:foo] = "encrypted_string" }

      it { subject.call; expect(response.cookies[:foo]).to eq(nil) } #passes
      it { subject.call; expect(cookies[:foo]).to eq(nil) }          #fails
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions