How to set cookies before request spec?

1,596 views
Skip to first unread message

Meltemi

unread,
Feb 15, 2012, 9:36:03 PM2/15/12
to rs...@googlegroups.com
I can't seem to figure out how to set a cookie for a Request spec?  Tried:

before(:each) do
  @user = Fabricate(:user)
  request.cookies[:id_token] = @user.id
end

but that gives a NoMethodError errors: undefined method `cookies' for nil:NilClass 

How does one run request specs when cookies need to be simulated?
Using Capybara if that matters

David Chelimsky

unread,
Feb 16, 2012, 7:59:19 AM2/16/12
to rspec-users

Request specs wrap Rails' integration tests, which simulate browser
interaction, but that's all. The idea is to not stub, simulate, etc,
anything internal at all. It's OK to create the `@user`, but cookies
should get set by making other requests that do so. Make sense?
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

George Dinwiddie

unread,
Feb 16, 2012, 12:58:14 PM2/16/12
to rspec-users
Meltemi,

On 2/15/12 9:36 PM, Meltemi wrote:
> I can't seem to figure out how to set a cookie for a Request spec? Tried:
>
> before(:each) do
> @user = Fabricate(:user)
> request.cookies[:id_token] = @user.id
> end
>
> but that gives a NoMethodError errors: undefined method `cookies' for
> nil:NilClass

In that snippit, "request" has not been constructed and therefore is nil.

- George

--
----------------------------------------------------------------------
* George Dinwiddie * http://blog.gdinwiddie.com
Software Development http://www.idiacomputing.com
Consultant and Coach http://www.agilemaryland.org
----------------------------------------------------------------------

Neill Pearman

unread,
May 27, 2014, 3:31:47 AM5/27/14
to rs...@googlegroups.com, rspec-users, li...@idiacomputing.com
Hi,

I've been trying to find out how to set a client-side cookie in a request spec, and this is the most relevant search result that I can find, so I hope that you don't mind me reviving the thread....

I want to write a request spec that simulates a request where a new cookie value has been set by the client and then sent to a particular Rails end point.  As the cookie is set by the client, I can't use a previous request to set the client.

Is it possible to send a client-side cookie value to a request spec?

Thanks,
Neill
Reply all
Reply to author
Forward
0 new messages