How do I check authlogic in rspec?

I can't seem to get my tests to work and I was wondering if anyone has pointers. I am testing my user edit page so you can login to be able to view and edit the user profile. According to the autologization documentation, here are the relevant codes:

class ApplicationController < ActionController::Base
  helper_method :current_user_session, :current_user

  private

def current_user_session
  return @current_user_session if defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.user
end

def require_current_user
  unless current_user
    flash[:error] = 'You must be logged in'
    redirect_to sign_in_path
  end
end
end

class UsersController < ApplicationController
  before_filter :require_current_user, :only => [:edit, :update]
  ...
end

      

In my users_controller_spec

describe "GET 'edit'" do

        before(:each) do
            @user = Factory(:user)
            UserSession.create(@user)
        end     

        it "should be successful" do
            # test for /users/id/edit
            get :edit, :id => @user.id
            response.should be_success
        end

      

I tested it with a browser and it works. You must be logged in to be able to edit your profile, but it doesn't work in my rspec test.

I have a feeling it has something to do with the mock controller , but I can't figure out how to do it. I also read the test case , but still can't get it to work.

Please help and thanks!

+3


source to share


1 answer


You can stub the current_user method on the ApplicationController, for example:



fake_user = controller.stub(:current_user).and_return(@user) #you could use a let(:user) { FactoryGirl.create(:user) } instead of before_each
get :edit, :id => fake_user.id
response.should be_success

      

+2


source







All Articles