[rspec-users] how to stub the :current_user and any methods associated with it

newbie alen198 at gmail.com
Tue Apr 15 19:08:45 EDT 2008


> Not sure why you are stubbing entries on the controller ... entries is
> called on the current user ...

I have this method. I want to use this variable to loop the users
@entries. So I'm testing to see that this variable is tested and works
correctly.

def main
    @entries = current_user.entries
end

I tried your suggestion
@current_user = mock_model(User, :id => 1, :entries => [@entry])

My error
"Mock 'User_1010' received unexpected message :entries with (no args)"

Maybe this is overtesting or not really necessary to test. Let me
know.

----------------------------------
I'm testing this out
----------------------------------
def main
    @entries = current_user.entries
end


------------------------------------------
Using this spec
------------------------------------------

before(:each) do
  @current_user = mock_model(User, :id => 1)
  @entry = mock_model(Entry, :user_id => 1)
  controller.stub!(:current_user).and_return(@current_user)
  controller.stub!(:login_required).and_return(:true)
  controller.stub!(:entries).and_return(@entry)
end

describe "on the main page" do
  it "should show all the current entries for the user" do
    @current_user = mock_model(User, :id => 1, :entries => [@entry])
    get :main
  end
end


More information about the rspec-users mailing list