本文介绍了如何存根 Restul-authentication 的 current_user 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试运行以下规范:
I'm trying to run the following spec:
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
我的控制器看起来像这样
My controller looks like this
def friends
@friends = current_user.friends
respond_to do |format|
format.html
end
end
问题是我不能存根 current_user 方法,因为当我运行测试时,我得到:
The problem is that I cannot stub the current_user method, as when I run the test, I get:
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 是来自 Restful-authentication 的一个方法,包含在这个控制器中.我应该如何测试这个控制器?
current_user is a method from Restful-authentication, which is included in this controller. How am I supposed to test this controller?
提前致谢
推荐答案
您需要模拟一个用户,然后将其传递给 login_as 方法以模拟用户登录.
You will need to mock a user and then pass it into the login_as method to simulate logging in the user.
@user_session = login_as(stub_model(User))
UserSession.stubs(:new).returns(@user_session)
http://bparanj.blogspot.com/2006_12_01_archive.html
这篇关于如何存根 Restul-authentication 的 current_user 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!