Open lefinal opened 7 months ago
Any updates on this?
This is already possible using the Unset
method, as shown in this example.
I just tried it in this sample test and it worked, this is the output:
=== RUN TestExample
prog_test.go:30: PASS: DoSomething(int,int,int)
prog_test.go:43: PASS: DoSomething(int,int,int)
--- PASS: TestExample (0.00s)
PASS
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 withOn
. The newly addedReplace()
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