qur / withmock

Automatic Go package mock generation tool
Other
71 stars 9 forks source link

Add support for mocked method but doesn't throw an assert error #20

Closed imosquera closed 10 years ago

imosquera commented 10 years ago

Currently if i mock a method using the standard

mockObj.EXPECT().MyMethod(myParam1).Return()

it will throw an error if MyMethod isn't called. But what if I would like mock a method but not confirm that it's called?

I'm proposing we create a WHEN() method.

mockObj.WHEN().MyMethod(myParam1).Return()

This just mocks the object but if it's not called during the test it won't throw an error.

Is this possible in the withmock project or will this have to be rewritten in the go-mock project?

qur commented 10 years ago

This would have to be supported by gomock, as that is where all the heavy lifting of the expectation management is done. The code generated by withmock is just a wrapper for Controller.Call() and Controller.RecordCall().

gomock already has .AnyTimes(), so you can do mockObj.EXPECT().MyMethod(myParam1).Return(nil).AnyTimes(). The gomock controller actually stores the min and max number of calls, so something like .MinCalls(0) should be possible ... no changes to withmock would be needed.

imosquera commented 10 years ago

Thanks Julian. This will work.