[rspec-users] mimicking a logged in user

S Ahmed sahmed1020 at gmail.com
Sat Mar 3 19:35:52 UTC 2012

I'm testing my controller, and confirming that a logged-in user can view
the page.

I have a session_helper that does this:

def signed_in?

And the current_user is set with:

def user_from_remember_token
    remember_token = cookies[:remember_token]
    User.find_by_remember_token(remember_token) unless remember_token.nil?

So in my controller, I need to fake a login somehow, how should I go about
doing this?

I'm not sure if this is the best place for it, but so far what I tried was
creating a method in my spec_helper's config block:

def test_sign_in(user)

(I get my user object from factorygirl).

So my controller_spec looks like:

describe "index" do
    before(:each) do
      @user = Factory(:user)

    it "should be successful" do
      get 'index'
      controller.current_user.should == @user
      response.should be_success

It is failing with current_user being nil.

Should I just somehow stub the call to 'user_from_remember_token' to return
the factorygirl user?
How can I do this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120303/8e31f1ad/attachment.html>

More information about the rspec-users mailing list