Rob Sanheim
2007-Apr-03 16:39 UTC
[mocha-developer] small suggestion - once method on Expectation
I find that a lot of times I want to ensure a method gets called exactly once, for example when testing caching or eager loading. What does the list think of adding this little humane method to Mocha::Expectation? def once times(1) self end which means this: def test_should_only_grab_post_once_from_the_cache Post.expects(:get_cache).with(google_vs_yahoo.id).times(1) comments.first.post comments.first.post end becomes: def test_should_only_grab_post_once_from_the_cache Post.expects(:get_cache).with(google_vs_yahoo.id).once comments.first.post comments.first.post end Not a huge difference, but a little nicer and ruby-ish in the same way we have Array.first, Array.last, etc. - Rob
David Chelimsky
2007-Apr-03 17:49 UTC
[mocha-developer] small suggestion - once method on Expectation
On 4/3/07, Rob Sanheim <rsanheim at gmail.com> wrote:> I find that a lot of times I want to ensure a method gets called > exactly once, for example when testing caching or eager loading. What > does the list think of adding this little humane method to > Mocha::Expectation? > > def once > times(1) > self > end > > which means this: > def test_should_only_grab_post_once_from_the_cache > Post.expects(:get_cache).with(google_vs_yahoo.id).times(1) > comments.first.post > comments.first.post > end > > becomes: > def test_should_only_grab_post_once_from_the_cache > Post.expects(:get_cache).with(google_vs_yahoo.id).once > comments.first.post > comments.first.post > end > > Not a huge difference, but a little nicer and ruby-ish in the same way > we have Array.first, Array.last, etc.Your suggestion might make things more clear, but once is implied w/ expects (and no times): def test_should_only_grab_post_once_from_the_cache Post.expects(:get_cache).with(google_vs_yahoo.id) comments.first.post comments.first.post end This would fail as you expect.> > - Rob > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >
James Mead
2007-Apr-03 18:19 UTC
[mocha-developer] small suggestion - once method on Expectation
David is right. I have occasionally thought about adding an "exactly_once" method for readability/emphasis. Is this what you have in mind? Or was it not clear from the documentation that the default was to expect a call exactly once? It would be good to have your feedback. -- James. http://blog.floehopper.org
Kevin Clark
2007-Apr-03 18:33 UTC
[mocha-developer] small suggestion - once method on Expectation
I find myself using .once here and there by accident. It''d be nice to have :) exactly_once work work too On 4/3/07, James Mead <jamesmead44 at gmail.com> wrote:> David is right. > > I have occasionally thought about adding an "exactly_once" method for > readability/emphasis. > > Is this what you have in mind? Or was it not clear from the documentation > that the default was to expect a call exactly once? > > It would be good to have your feedback. > -- > James. > http://blog.floehopper.org > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >-- Kevin Clark http://glu.ttono.us
Rob Sanheim
2007-Apr-03 18:40 UTC
[mocha-developer] small suggestion - once method on Expectation
On 4/3/07, Kevin Clark <kevin.clark at gmail.com> wrote:> I find myself using .once here and there by accident. It''d be nice to have :) > exactly_once work work too >Yeah, I didn''t realize .once was implicit, and I''ve often tried to use .once since it seems like it would be consistent with never/times/etc. I think having the explicit call available for readability would be great in addition to having a note about the implicit nature of it in the examples somewhere. The examples are very useful btw - I always refer to them from w/i textmate when I forget how to call something. Documentation right there w/i the code is much better then having to drop out to google to look things up. thanks, Rob