spec/controller/something_controller_spec.rb
context "test requiring signed cookie" do
it "will receive 200 OK" do
cookies.signed["foo"] = "bar"
post '/path/to/somewhere', params: params
expect(response.status).to eql(200)
end
end
上記のような spec case 内で cookies.signed["foo"]
などとすると、
stdout
NoMethodError: undefined method `signed' for #<Rack::Test::CookieJar:0x000...>
上記のようなエラーが出てしまう。
これは RSpec 内で cookie を保持するインスタンスである Rack::Test::CookieJar
が signed な cookie をサポートしていないのが原因である。
signed な cookie を使いたい場合は ActionDispatch::Cookies::CookieJar
を cookies に代入してやればよい。具体的には以下のようにする。
spec/controller/something_controller_spec.rb
context "test requiring signed cookie" do
it "will receive 200 OK" do
test_cookies = ActionDispatch::Request.new(Rails.application.env_config.deep_dup).cookie_jar
test_cookies.signed["foo"] = "bar"
cookies["foo"] = test_cookies["foo"]
post '/path/to/somewhere', params: params
expect(response.status).to eql(200)
end
end
spec case で全体的に signed cookie を参照するような場合は helper method にしてもよい。