views:

94

answers:

1

Hi there,

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

A: 

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

ThinkBohemian
I've tried:<pre> it "should call current_user.friends" do user = mock_model(User) user.should_receive(:friends) @user_session = login_as(user) get :friends end</pre>But it also didn't work. I thought that if I only call login_as it would be solved. Why's that?
Thiago
you may also need to call:UserSession.stubs(:new).returns(@user_session)(or rename UserSession to the session model that restful-authentication created to handle sessions)
ThinkBohemian