[rspec-users] Cookies in RSpec

Priit Tamboom priit.tamboom at eesti.ee
Fri Oct 5 03:05:51 EDT 2007


On 10/4/07, Nathan Sutton <nathan.sutton at gmail.com> wrote:
> So how do you work with cookies properly in rspec now?  I noticed in
> the docs that it mentions session, assigns, and flash, but nothing of
> cookie.  I'm using edge rails so I'm concerned about changes to the
> cookie mechanisms.  I need to assign values into the cookie (a
> remember token for restful authentication) so that I can have it log
> in by cookie.  here is my spec for it:
>
> http://pastie.caboo.se/103647
>
> I'm using AR-stored-sessions, if that matters.
>
> Nate
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>

Hi Nate,

I'm mocking cookies like this:

  it "should clean cookie with remember_token" do
    cookies = mock('cookies')
    cookies.stub!(:[])
    controller.stub!(:cookies).and_return(cookies)

    cookies.should_receive(:delete).with(:remember_token)
    delete :destroy
  end


   def destroy
    current_user.forget_me
    cookies.delete :remember_token
    reset_session
    redirect_to home_path
  end


You can stub writing part like this:
   cookies.stub!(:[]=)

I have got some nice ideas from caboo's empty rspec app:
http://plugins.svn.caboo.se/browser/court3nay/empty_apps/restful_auth_rspec

Priit


More information about the rspec-users mailing list