2012-03-28 6 views
3

Я не могу заставить свои тесты работать, и мне было интересно, есть ли у кого-нибудь указатели. Я тестирую страницу редактирования своего пользователя, чтобы вы могли войти в систему, чтобы иметь возможность просматривать и редактировать профиль пользователя. После authlogic документации, вот соответствующие коды:Как проверить authlogic в rspec?

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 

В моем 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 

Я тестировал его с помощью браузера, и она работает. Вы должны войти в систему, чтобы иметь возможность редактировать свой профиль, но он не работает в моем тесте rspec.

У меня такое чувство, что оно имеет какое-то отношение к mock controller, но я не могу понять, как это сделать. Я также читал test case, но до сих пор не могу заставить его работать.

Пожалуйста, помогите и спасибо!

ответ

2

Вы можете окурок метод current_user на ApplicationController так:

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