assert_no_cookie dammit

28 August 2006

I hate cookies. Or maybe I’m just feeling schizo today. Actually, I do love cookies, just not testing them with Rails.

I’ve added assert\_no\_cookie method to the assert\_cookie plugin. That’s because in your functional tests, cookies['chocolate'] may be something, nil, or an empty array depending on what you’ve just done. For example, in this code I had tried assert\_nil cookies['pass'] but that failed because cookies['pass'] was [] after calling cookies.delete :pass in the controller:

<typo:code lang="ruby">
def test_destroy_should_remove_persistent_login_cookie
  post :create, :username => '', :password => 'secret', 
      :remember_me => '1'
  pass = cookies['pass']
  @request.cookies['pass'] = cookies['pass']
  post :destroy, :_method => 'delete'
  assert_no_cookie :pass
  user = User.find_by_username ''
  assert_nil user.persistent_logins.find_by_pass(pass)

Anyway, as before, you can get this by script/plugin

And thanks yet again to Pluit Solutions for helping me keep my head straight about how to work with cookies in functional tests. Next stop, a patch for Rails (would a generous soul please donate some time to this so I don’t have to).