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.