Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mock): add Replace method to Call #1511

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lefinal
Copy link

@lefinal lefinal commented Nov 29, 2023

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 calling Unset() first, and then On with the call's method and arguments.

This added test is also an example:

func Test_Mock_Replace(t *testing.T) {
	var mockedService = &TestExampleImplementation{}

	mockedService.On("TheExampleMethod", 1, 2, 3).Return(1, nil)
	mockedService.On("TheExampleMethod", 1, 2, 3).Replace().Return(2, nil)

	res, _ := mockedService.TheExampleMethod(1, 2, 3)
	assert.Equal(t, 2, res, "should return correct value")
}

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

The Replace method on *mock.Call 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.
@lefinal lefinal changed the title feat(mock): add Replace method feat(mock): add Replace method to Call Nov 29, 2023
@lefinal
Copy link
Author

lefinal commented Dec 15, 2023

Any updates on this?

@st3penta
Copy link

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

@dolmen dolmen added enhancement pkg-mock Any issues related to Mock labels Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement pkg-mock Any issues related to Mock
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants