This is a problem with the cookies collection in a controller spec in the following environment:
This is how I solved this:
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
current_user = user
@current_user = user
end
def sign_out
current_user = nil
@current_user = nil
cookies.delete(:remember_token)
end
def signed_in?
return !current_user.nil?
end
For some reason you have to set both @current_user and current_user for it to work in Rspec. I'm not sure why but it drove me completely nuts since it was working fine in the browser.
The extra lines above are not necessary. I found that simply calling the self.current_user = user setter method can automatically update the instance variable. For some reason, calling it without self doesn't call the setter method.
Here's my code without the extra lines:
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end
def current_user=(user)
@current_user = user
end
I still don't know why, maybe an Rspec bug
Ran into the same problem. Try using @request.cookie_jar
from your tests.