我正在尝试运行以下规范:
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的方法,它包含在这个控制器中。我该如何测试这个控制器呢?
提前感谢
发布于 2010-05-13 03:11:14
您将需要模拟一个用户,然后将其传递给login_as方法,以模拟登录该用户。
@user_session = login_as(stub_model(User))
UserSession.stubs(:new).returns(@user_session)http://bparanj.blogspot.com/2006_12_01_archive.html
https://stackoverflow.com/questions/2821841
复制相似问题