I have general application behavior that changes based on cookie contents, and need to test it. As an example, if a user doesn't have a cookie set indicating that they've accepted the site's legal terms, they need to be redirected to the T&C page.
I can set a cookie in a functional test using
request.cookies["legal_accepted"] = "yes"
However, this will not work in an integration test- there's no request object to work with. I've been unable to find any documentation of why this is so, what else is different, and best practices for working with the difference. How can I set a cookie for a given request? Is the why and wherefore documented anywhere?
A Rails 6 system test is a test that exercises your application in a way that, as much as possible, simulates a real user interacting with it via a browser. More than any other kind of tests, system tests verify that the whole app does what it's supposed to do.
The only way I have found to do this is to set the cookie header manually in the request, e.g.
get "/", {}, { "HTTP_COOKIE" => "legal_accepted=yes; cookie2=value2; "}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With