Hi, It appears that if I have 2 should_receives in a row, the latest one overrides the previous one(s). If there isn''t one, could we add a way to accumulate them, such as @thing.should_receive(:method).and_return(@value) @thing.should_receive(:method).again.and_return(@value) @thing.should_receive(:method).again.and_return(@value) would be equivalent to @thing.should_receive(:method).times(3).and_return(@value) This would be helpful because I have a setup method that sets up the should_receive once, and then some specific actions may use that method one or more additional times. thanks linoj
On 6/3/07, Jonathan Linowes <jonathan at parkerhill.com> wrote:> Hi, > > It appears that if I have 2 should_receives in a row, the latest one > overrides the previous one(s). > > If there isn''t one, could we add a way to accumulate them, such as > > @thing.should_receive(:method).and_return(@value) > @thing.should_receive(:method).again.and_return(@value) > @thing.should_receive(:method).again.and_return(@value) > > would be equivalent to > > @thing.should_receive(:method).times(3).and_return(@value) > > This would be helpful because I have a setup method that sets up the > should_receive once, and then some specific actions may use that > method one or more additional times. >@thing.should_receive(:method).exactly(3).times.and_return(@value) http://rspec.rubyforge.org/rdoc/classes/Spec/Mocks.html Aslak> thanks > linoj > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 6/3/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> On 6/3/07, Jonathan Linowes <jonathan at parkerhill.com> wrote: > > This would be helpful because I have a setup method that sets up the > > should_receive once, and then some specific actions may use that > > method one or more additional times. > > @thing.should_receive(:method).exactly(3).times.and_return(@value) > http://rspec.rubyforge.org/rdoc/classes/Spec/Mocks.htmlYou might also consider simply stubbing the method in your setup, and only specifying a number of times called in specific examples. Whenever reasonable, I try to follow the advice given by David Chelimsky and aim for no mock expectations in the setup and only one per example: http://blog.davidchelimsky.net/articles/2006/11/09/tutorial-rspec-stubs-and-mocks Also, you might want to think about whether or not you really should be locking down your specification to exactly three (or however many) calls. Without knowing your details I can''t say for certain if this applies to you, but often the exact number of calls is an implementation detail that your spec doesn''t need to worry about. If that''s the case, I might loosen up the mock expectation a little bit by simply saying @thing.should_receive(:method).at_least(:once).and_return(@value) -- Nick
The issue was the number of calls. I really only want to count the number of calls by the specific action. But there''s a before filter which calls the same method. I couldn''t stub out the before filter itself because it sets an instance variable used by the action. Does that make sense? So my example has to know more about the implementation than I would like. On Jun 3, 2007, at 10:19 PM, nicholas a. evans wrote:> On 6/3/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote: >> On 6/3/07, Jonathan Linowes <jonathan at parkerhill.com> wrote: >>> This would be helpful because I have a setup method that sets up the >>> should_receive once, and then some specific actions may use that >>> method one or more additional times. >> >> @thing.should_receive(:method).exactly(3).times.and_return(@value) >> http://rspec.rubyforge.org/rdoc/classes/Spec/Mocks.html > > You might also consider simply stubbing the method in your setup, and > only specifying a number of times called in specific examples. > Whenever reasonable, I try to follow the advice given by David > Chelimsky and aim for no mock expectations in the setup and only one > per example: http://blog.davidchelimsky.net/articles/2006/11/09/ > tutorial-rspec-stubs-and-mocks > > Also, you might want to think about whether or not you really should > be locking down your specification to exactly three (or however many) > calls. Without knowing your details I can''t say for certain if this > applies to you, but often the exact number of calls is an > implementation detail that your spec doesn''t need to worry about. If > that''s the case, I might loosen up the mock expectation a little bit > by simply saying > @thing.should_receive(:method).at_least(:once).and_return(@value) > > -- > Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users