I have a helper method def login_as(role) user = stub_model(User) user.stub(:is_administrator?).and_return(role == :admin) User.stub(:find_by_id).and_return(user) session[:user_id] = user.id user end which has been dandy until yesterday. I am now working on an admin controller for user maintenance, and in it, I want to do this: before :each do login_as(:admin) @some_user = stub_model(User, valid_user_hash) User.stub(:find_by_id).and_return(@some_user) end which obviously conflicts with the stub in login_as. I have experimented with adding .with() to each one, hoping that multiple stubs would be created, but that does not seem to be the case. Is there a way to stub the same method but have it return different values? Or can someone suggest a better way of handling this situation? Peace, Phillip
On Apr 30, 2010, at 11:34 AM, Phillip Koebbe wrote:> I have a helper method > > def login_as(role) > user = stub_model(User) > user.stub(:is_administrator?).and_return(role == :admin) > User.stub(:find_by_id).and_return(user) > session[:user_id] = user.id > user > end > > which has been dandy until yesterday. I am now working on an admin controller for user maintenance, and in it, I want to do this: > > before :each do > login_as(:admin) > @some_user = stub_model(User, valid_user_hash) > User.stub(:find_by_id).and_return(@some_user) > end > > which obviously conflicts with the stub in login_as. > > I have experimented with adding .with() to each one, hoping that multiple stubs would be created, but that does not seem to be the case. Is there a way to stub the same method but have it return different values? Or can someone suggest a better way of handling this situation?Two ways to do it: 1 is documented here; http://rspec.info/documentation/mocks/stubs.html The other: User.stub(:find_by_id) do |id| if id == $admin_id admin_user else non_admin_user end end or some such. HTH, David> > Peace, > Phillip > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100430/4494aef1/attachment.html>
On 2010-04-30 11:34 AM, Phillip Koebbe wrote:> I have a helper method > > def login_as(role) > user = stub_model(User) > user.stub(:is_administrator?).and_return(role == :admin) > User.stub(:find_by_id).and_return(user) > session[:user_id] = user.id > user > end > > which has been dandy until yesterday. I am now working on an admin > controller for user maintenance, and in it, I want to do this: > > before :each do > login_as(:admin) > @some_user = stub_model(User, valid_user_hash) > User.stub(:find_by_id).and_return(@some_user) > end > > which obviously conflicts with the stub in login_as. > > I have experimented with adding .with() to each one, hoping that > multiple stubs would be created, but that does not seem to be the > case. Is there a way to stub the same method but have it return > different values? Or can someone suggest a better way of handling this > situation? >To answer my own question, I discovered I could stub @controller in login_as: @controller.stub(:current_user).and_return(user) which works. Can anyone think of a reason I would not want to do that? Peace, Phillip