Occasionally, I find myself in a situation where I want to have a mock
obj returned if a method is called with a particular argument but
handled normally otherwise.  For example, lets say I have a Model
named User and I am specing a controller that sends messages from one
user to another.  When User.find is called for the user who is making
the request I want it to run normally but when User.find is called for
the receiver I want it to return a mocked obj.  In this case, I can do
something like (http://gist.github.com/352305):

user = mock_model(User)
User.stub!(:find).at_least(1).and_return do |id|
    if id == mock_user.id.to_s
      user
    else
      User.find_by_id(id)
    end
end

If I didn't have another method that allowed me to find a User by it's
id this won't work.

Is there an easier way to accomplish this?

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to