How to set cookie for integration test in Rails?

I have a general application behavior that changes based on cookie content and you need to test it. As an example, if a user does not have a set of cookies indicating that they have accepted the terms of the site, they need to be redirected to the T & C.

I can set a cookie in a functional test using

request.cookies["legal_accepted"] = "yes" 

However, this will not work in the integration test - there is no request object there. I could not find the documentation on why this is so, what is not yet, and best practices for dealing with the difference. How to set a cookie for this request? Why and where is it documented?

+6
source share
3 answers

The only way I found this is to set the cookie header manually in the request, for example.

 get "/", {}, { "HTTP_COOKIE" => "legal_accepted=yes; cookie2=value2; "} 
+5
source

In Rails 3.2.x (specifically in 3.2.16), you can set cookies directly in the integration test:

 cookies['foo'] = 'bar' 

Now you can access the cookie 'foo' in your test and application:

 puts cookies['foo'] (returns 'bar') 
+2
source

If you want to use Poltergeist, there are some methods of processing cookies that I used in integration tests.

https://github.com/teampoltergeist/poltergeist#manipulating-cookies

It should be noted that if you want to update the cookie, first delete the cookie and then set it.

 page.driver.remove_cookie("cookie_name") page.driver.set_cookie("cookie_name", "some_value") 
0
source

All Articles