[mocha-developer] small suggestion - once method on Expectation

David Chelimsky dchelimsky at gmail.com
Tue Apr 3 13:49:19 EDT 2007


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
>


More information about the mocha-developer mailing list