<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Waage Blog &#187; persistent cookies</title>
	<atom:link href="http://qugstart.com/blog/tag/persistent-cookies/feed/" rel="self" type="application/rss+xml" />
	<link>http://qugstart.com/blog</link>
	<description>Ruby, Rails, Life</description>
	<lastBuildDate>Thu, 10 Nov 2011 00:35:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Functional Testing Cookies in Ruby on Rails 1.2.3</title>
		<link>http://qugstart.com/blog/ruby-and-rails/functional-testing-cookies-in-ruby-on-rails-123/</link>
		<comments>http://qugstart.com/blog/ruby-and-rails/functional-testing-cookies-in-ruby-on-rails-123/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 07:33:18 +0000</pubDate>
		<dc:creator>Andrew Waage</dc:creator>
				<category><![CDATA[Rails Testing]]></category>
		<category><![CDATA[Ruby and Rails]]></category>
		<category><![CDATA[assert_cookie]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[functional testing]]></category>
		<category><![CDATA[persistent cookies]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://qugstart.com/blog/?p=5</guid>
		<description><![CDATA[After spending a good couple hours + trying to figure out why my functional tests were failing when working with cookies, I came across a couple great resources for rails + testing cookies.
Here&#8217;s a good basic explanation of using cookies on Pluit solutions website.  Then I came across the Robby on Rails blog that [...]]]></description>
			<content:encoded><![CDATA[<p>After spending a good couple hours + trying to figure out why my functional tests were failing when working with cookies, I came across a couple great resources for rails + testing cookies.</p>
<p>Here&#8217;s a good basic explanation of using cookies on <a title="Pluit Solutions" href="http://www.pluitsolutions.com/2006/08/02/rails-functional-test-with-cookie/">Pluit solutions</a> website.  Then I came across the <a title="Robby on Rails" href="http://www.robbyonrails.com/articles/2006/08/28/testing-cookies-in-ruby-on-rails">Robby on Rails</a> blog that led me to understand (and misunderstand) the <a title="assert_cookie" href="http://blog.brightredglow.com/articles/2006/08/27/assert_cookie-for-ooey-gooey-fun">asssert_cookie</a> plugin.</p>
<p>However, I was still having a lot of trouble figuring out why I was setting my @request.cookies['foo'] in my functional test, but later (in the same test method) checking for cookies['foo'] and not finding anything in the cookie jar!</p>
<p>I then tried setting cookies['foo'] in my controller test, but found that when I did a check for the existance of that cookie in my actual controller code, it was not there! I thought that I should be able to set up a cookie in a test and have my controller recognize it. This is true, however, not by setting cookies['foo'].</p>
<p>So&#8230; I opened up a blank controller and functional test and began testing out different combinations to find out exactly what was going on. I hope this little example helps to save someone else&#8217;s precious time. Here it goes&#8230;</p>
<p>This is the controller I will be referring to for my example tests:</p>
<pre lang="rails" class="prettyprint">========= controller
  def foo
    if cookies['oreo'] == 'yesitis'
      @info =  "Oreos are tasty"
    else
      @info = "Only Milk here"
    end
     render :nothing => true
  end</pre>
<p>1. In your functional test, do not test for presence of a cookie after a get request to a particular action, <strong>unless </strong>the action you are testing specifically sets up that cookie.  In other words, if your controller action is not setting the cookie, do not test for that cookie&#8217;s presence.</p>
<p>In the above controller, no cookies are ever being set. Therefore, testing for the presence of a cookie after the get request (in a test method) will return false.</p>
<p>In other words, the cookie is recognized by the controller, however, notice that cookies['oreo'] is nil after the get request:</p>
<pre lang="rails" class="prettyprint">  def test_oreo
    @request.cookies['oreo'] = CGI::Cookie.new("oreo", "yesitis")
    get :foo
    assert_equal({}, @response.cookies)
    assert_equal(nil, cookies['oreo'])
    #Assert cookie will be nil here so do not test it:
    #assert_cookie <img src='http://qugstart.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> reo
    puts assigns['info'] #"Oreos are tasty"
  end</pre>
<p>Note:<br />
1. After your get request, the @response.cookies will be empty<br />
2. After your get request, cookies['oreo'] will still be nil.<br />
3. After your get request, you cannot use assert_cookie because it will be nil</p>
<p>2. If your controller action expects a cookie to be there, you <strong>cannot </strong>set it up in your functional test by assigning:</p>
<li> cookies['oreo'] = true  (<span style="color: #ff0000;"><em>incorrect</em></span>) OR</li>
<li>@request.cookies['oreo'] = true   (<span style="color: #ff0000;"><em>incorrect</em></span>)</li>
<p>The only way to set up your cookie for a subsequent request, so that your controller will see it is:</p>
<ul>
<li>@request.cookies['oreo'] = CGI::Cookies.new(&#8217;oreo&#8217;, true)    (<span style="color: #339966;">correct</span>)</li>
</ul>
<pre lang="rails" class="prettyprint">  #INCORRECT
  def test_oreo
    cookies['oreo'] = 'yesitis'
    get :foo
    puts assigns['info'] #"Only Milk here"
  end

#INCORRECT
  def test_oreo
    cookies['oreo'] =  CGI::Cookie.new("oreo", "yesitis")
    get :foo
    puts assigns['info'] #"Only Milk here"
  end

#INCORRECT
  def test_oreo
    @request.cookies['oreo'] = 'yesitis'
    get :foo
    puts assigns['info'] #"Only Milk here"
  end</pre>
<p><strong>The only way to set up your cookie in a functional test and have your controller recognize it.</strong></p>
<pre lang="rails" class="prettyprint">#CORRECT
  def test_oreo
    @request.cookies['oreo'] = CGI::Cookie.new("oreo", "yesitis")
    get :foo
    puts assigns['info'] # Will print out "Oreos are tasty"
  end</pre>
<p>3. Avoid the temptation to use symbols in your tests with cookies.</p>
<ul>
<li> cookies[:foo]   -> BAD</li>
<li> cookies['foo']   -> GOOD</li>
</ul>
<p>4. Only use <strong>assert_cookie</strong> (plugin: see above for link) if you are testing that the controller action is creating a new cookie.</p>
<p>5. If you are testing multiple actions and redirects from one action to another action in the same controller, you do not need to set the cookie before each action, explicitly. The cookie will remain in the @request object that you created in the setup method of your test.</p>
<p>EXAMPLE TWO (Testing persistent cookies)<br />
You DONT set up the cookie for each subsequent request.<br />
It does &#8220;persist&#8221; throughout your test-method because you are<br />
using the same @request object!!</p>
<pre lang="rails" class="prettyprint">========controller
  def foo
    if cookies['oreo'] == 'yesitis'
	puts "Foo has OREO"
    else
 	puts "Foo has NOTHING"
    end
      redirect_to :action => :bar
      return true
  end

  def bar
    if cookies['oreo'] == 'yesitis'
      puts "Oreos are tasty"
    else
      puts "Only Milk here"
    end
     render :nothing => true
  end

========= Test
  def test_oreo
   @request.cookies['oreo'] =  CGI::Cookie.new('oreo', "yesitis")
    get :foo    #OUTPUTS: Foo has OREO

    # COOKIE PERSISTS HERE!!
    # Don't need to explicitly set cookie again in the
    # @request.cookies object
    get :bar   #OUTPUTS: Oreos are tasty
  end</pre>
]]></content:encoded>
			<wfw:commentRss>http://qugstart.com/blog/ruby-and-rails/functional-testing-cookies-in-ruby-on-rails-123/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

