Using cookies in RSpec tests

I spent way too long today figuring out why a test for my update method was failing. Short answer: RSpec cookies don’t play nicely with Rails 3 testing, or at least not with Rails 3.1.rc5.

The relevant bits of the final code, with comments, can be viewed in this gist.

I first had to figure out why I was getting a nil.update_attributes error. I ruled out the absence of my record, then lost time thinking that the test was somehow losing the hash of values I wanted to update to, before realizing that the big difference between my test and my actual code (which was already working fine) was the absence of the cookie used by the update function to find the correct record.

The next big hurdle was finding out why cookies[:subdomain_name] = "my_subdomain" didn’t actually set anything, or at least anything I could retrieve. This thread from ruby-forum.com was crucial in helping me understand what was happening, and two threads on StackOverflow helped me figure out the syntax I needed.

Advertisements

About joanwolk

3 things I like: theater, food, geekery. 3 things I don't like: clichés, blisters, coffee.
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Using cookies in RSpec tests

  1. Raphael Lee says:

    Thanks for this post. You saved my butt.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s