S Ahmed
2011-Jun-17 23:17 UTC
[rspec-users] 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? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110617/5714626a/attachment.html>
Patrick J. Collins
2011-Jun-17 23:39 UTC
[rspec-users] how to mock/stub when there are multiple model objects?
> 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
S Ahmed
2011-Jun-23 20:55 UTC
[rspec-users] how to mock/stub when there are multiple model objects?
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 < patrick at collinatorstudios.com> wrote:> > 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 > _______________________________________________ > 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/20110623/c0232589/attachment.html>
David Chelimsky
2011-Jun-27 10:50 UTC
[rspec-users] how to mock/stub when there are multiple model objects?
On Jun 23, 2011, at 3:55 PM, S Ahmed wrote:> 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?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?> > On Fri, Jun 17, 2011 at 7:39 PM, Patrick J. Collins <patrick at collinatorstudios.com> wrote: > > 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersCheers, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110627/6232c353/attachment-0001.html>