Reputation: 473
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?
Upvotes: 14
Views: 6029
Reputation: 531
For Rails 5 integration tests metkat's answer should be altered slightly:
get "/", headers: {"HTTP_COOKIE" => "legal_accepted=yes; cookie2=value2;"}
Upvotes: 8
Reputation: 1200
If you want to use Poltergeist there are some methods for handeling cookies that I've used in integration tests.
https://github.com/teampoltergeist/poltergeist#manipulating-cookies
One thing to note is that if you want to update a cookie, you'll have remove the cookie first, then set it.
page.driver.remove_cookie("cookie_name")
page.driver.set_cookie("cookie_name", "some_value")
Upvotes: -1
Reputation: 372
In Rails 3.2.x (certainly in 3.2.16) you can assign the cookies directly in an integration test:
cookies['foo'] = 'bar'
You can now access the cookie 'foo' in your test and application:
puts cookies['foo'] (returns 'bar')
Upvotes: 7
Reputation: 473
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; "}
Upvotes: 6