我正在尝试运行以下规范:
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是来自Restful-authentication的方法,它包含在此控制器中。我该如何测试这个控制器?
提前致谢
答案 0 :(得分:1)
您需要模拟用户,然后将其传递给login_as方法以模拟用户的日志记录。
@user_session = login_as(stub_model(User))
UserSession.stubs(:new).returns(@user_session)