I have recently been in the situation where I was testing a controller that looked up the value of a cookie in a before_filter to validate the user.

I had trouble setting the cookie from within the controller spec so that it was available to the beforefilter. The solution that I want to share is

controller.stubs(:cookies).returns({:usersession => 'xxxx'})

The reason I could not get this working earlier was that I was mistakenly trying to stub the cookies method directly. Instead I have to stub the controller cookies, even if it is a private method.