feat(mock): add Replace method to Call #1511
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The Replace function allows for existing mock handlers to be replaced with new mock handlers, essentially allowing for the same method to be called with different return values. This increases flexibility and functionality in testing scenarios.
Changes
Added
Replace()
method to*mock.Call
as well as a few tests.Motivation
A possible use-case of mock is specifying all normally expected calls in a test setup.
These calls return default or "correct" values and use
.Maybe()
.If scenarios are tested, where a mocked object returns other values, one currently needs to unset the call and then create a new one with
On
.The newly added
Replace()
method is syntactic sugar for this use-case by callingUnset()
first, and thenOn
with the call's method and arguments.This added test is also an example:
The first call to
On
might be located in a test setup for the "good route".The second one in a test for some specific functionality that relies on the service returning different values or errors.
Related issues
none