Support stacking mocks on top of real implementations
Allow stacking mocks on top of real implementations (or other mocks). If a call doesn't match an expectation in one layer, it can defer the call to the next layer. If none of the layers expect such a call, the call can be deemed unexpected and trigger a failure.
One use case of this is for error injection in tests. A test can stack a mock on top of a real implementation and inject errors. This can be done with gomocks today as well, but it requires the test to mock any/all other methods it may invoke on the mock. The real implementation backing the mock can provide the "default" implementation the test needs.
One way to implement this is for mockgen to (optionally) build stackable mocks such that
- The mock constructors take an optional backing implementation.
- If a call is expected, it is handled as it is today.
- If a call is not expected and a backing implementation is available, the call is "forwarded" to the backing implementation
- If a call is not expected and a backing implementation is not available, the call is handled as an unexpected call is handled today.
I have a PR in for fork of this repo which such an implementation along with examples: https://github.com/chawld/mock/pull/1
Source: golang/mock