This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
Proposal: Provide an option where every invocation of EXPECT()
of a mock should override any previous invocations.
#685
Labels
Hi team,
The proposal here is to allow for an option to bring GoMock closer in line with other mocking libraries that exist for other languages. In particular, the GoMock behavior of ordering mock expectations in a FIFO manner makes it difficult to bundle default mock expectations into a helper or testify Suite's BeforeTest block.
The problem
For example, the following set up won't work:
Instead, one must do something like this:
It's not hard to imagine that the test body can really blow up once the number of dependencies one needs to mock out grows in size.
Examples from other languages
Some popular mocking frameworks in other languages support setting default mock behaviors, which helps clean up the test code:
Proposal
Provide an option where every invocation of
EXPECT()
of a mock should override any previous invocations.How to address returning different values based on input?
Use
DoAndReturn
to handle different argsExample Pull Request
#686
Alternatives Considered
There have been several previous issues filed that proposed a reverse ordering strategy for mock expectations:
However, a couple usability problems arise with a reverse ordering strategy. One is that if one were to put a mock expectation in a
SetupTest
that runs before each test, it's implied that one must appendAnyTimes()
to the end of it. This is because if one were to leave that out, the gomock controller would continue to wait for for that expectation, even if one were to "override" it within the test block.The text was updated successfully, but these errors were encountered: