How to stub Restul-authentication's current_user method?
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
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 is a method from Restful-authentication, which is included in this controller. How am I supposed to test this controller?
Thanks in advance
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
精彩评论