2010-05-12 18 views
0

我試圖運行以下規格:如何刪除Restul-authentication的current_user方法?

describe UsersController, "GET friends" do 

    it "should call current_user.friends" do 
    user = mock_model(User) 
    user.should_receive(:friends) 
    UsersController.stub!(:current_user).and_return(user) 
    get :friends 
    end 

end 

我的控制器看起來像這樣

def friends 
    @friends = current_user.friends 
    respond_to do |format| 
     format.html 
    end 
    end 

的問題是,我不能存根CURRENT_USER方法,當我運行測試的,我得到:

Spec::Mocks::MockExpectationError in 'UsersController GET friends should call current 
_user.friends' 
Mock "User_1001" expected :friends with (any args) once, but received it 0 times[0m 
./spec/controllers/users_controller_spec.rb:44: 

CURRENT_USER是從寧靜的認證,其中包含在該控制器的方法。我應該如何測試這個控制器?

在此先感謝

回答

1

您將需要模擬一個用戶,然後將它傳遞到login_as方法在用戶模擬記錄。

@user_session = login_as(stub_model(User)) 
UserSession.stubs(:new).returns(@user_session) 

http://bparanj.blogspot.com/2006_12_01_archive.html

+0

我已經試過:

 it "should call current_user.friends" do user = mock_model(User) user.should_receive(:friends) @user_session = login_as(user) get :friends end 
但它也沒有工作。我認爲如果我只調用login_as就可以解決問題。爲什麼? – Thiago 2010-05-12 20:28:46

+0

您可能還需要撥打: UserSession.stubs(:new).returns(@user_session) (或將UserSession重命名爲會話模型,該會話模型使用restful-authentication創建來處理會話) – Schneems 2010-05-12 22:22:00