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

Rob Sanheim rsanheim at gmail.com
Tue Apr 3 12:39:33 EDT 2007


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


More information about the mocha-developer mailing list