views:

264

answers:

1

I just started using mocha and I find it annoying that when creating a new mock object, mocha expects it to be called exactly once. I have helper methods to generate my mocks and I'm doing something like this

my_mock = mock(HashOfParameters)

All of the parameters might not get called for each test method so it will raise an error:

expected exactly once, not yet invoked

So I figured I needed to do something like this:

my_mock = mock()
HashOfParameters.each do |k, v|
  my_mock.expects(k).returns(v).at_least(0)
end

This works but I was wondering if there was an easier way to do this, like changing a default configuration somewhere...

+2  A: 

Ok, that was a stupid question... I hadn't took the time to truly understand the difference between a mock and a stub. Here's a good article that shows how it works :

http://martinfowler.com/articles/mocksArentStubs.html

So in my example, I should have been using the stub method instead of mock.

Pierre Olivier Martel