How to mock/stub when there are multiple model objects?

I’m a bit confused how to hook into a particular model object instance
and
mock/stub it?

I understand how to hook into a class level method like:

User.should_receive(:where).and_return(…)

but what if my method looks like:

def some_thing

user = User.where("…")

user2 = User.where("…")

user.delete

user2.update_attributes(some_attr)

end

How can I hook into one or both model instances of ‘user’ and ‘user2’ at
the
same time in the same test?

Or is it only possible to do it one at a time?

Thanks.

I’m trying this now on a Model that is using Mongoid.

I have tried:

it “should …” do

User.stub(:some_call).and_return(User.new(:name => “blah”))

end

And when running spec on this file I get:

NoMethodError, undefined method “stub” for User:Class

I also tried:

User.stub!(:some_call)…

Does this not work on mongoid objects?

On Fri, Jun 17, 2011 at 7:39 PM, Patrick J. Collins <

On Jun 23, 2011, at 3:55 PM, S Ahmed wrote:

end

And when running spec on this file I get:

NoMethodError, undefined method “stub” for User:Class

I also tried:

User.stub!(:some_call)…

Does this not work on mongoid objects?

It should work on any object (class objects and instance objects), so it
sounds like there is a configuration problem. What’s in your Gemfile?

http://collinatorstudios.com


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Cheers,
David

but what if my method looks like:

user = User.where(“…”)
user2 = User.where(“…”)
User.should_receive(:where).with(“…”).and_return(…)
http://apidock.com/rspec/Spec/Mocks

Patrick J. Collins
http://collinatorstudios.com