Skip to content

Commit

Permalink
feat(mock): add Replace method to Call
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lennart Altenhof committed Nov 29, 2023
1 parent db8608e commit f182530
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
10 changes: 10 additions & 0 deletions mock/mock.go
Expand Up @@ -252,6 +252,16 @@ func (c *Call) Unset() *Call {
return c
}

// Replace removes mock handlers from being called and returns a new Call. The
// behavior is similar to calling Unset and then On again with the same method
// name and arguments.
//
// test.On("func", mock.Anything).Replace().Return(true)
func (c *Call) Replace() *Call {
c.Unset()
return c.Parent.On(c.Method, c.Arguments...)
}

// NotBefore indicates that the mock should only be called after the referenced
// calls have been called as expected. The referenced calls may be from the
// same mock instance and/or other mock instances.
Expand Down
47 changes: 47 additions & 0 deletions mock/mock_test.go
Expand Up @@ -666,6 +666,53 @@ func Test_Mock_Unset_WithFuncPanics(t *testing.T) {
})
}

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")
}

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

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

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

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

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

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

mockedService.On("TheExampleMethod", 4, 4, 4).Return(5, nil).
On("TheExampleMethod", 3, 3, 3).Replace().Return(10, nil)

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

func Test_Mock_Return(t *testing.T) {

// make a test impl object
Expand Down

0 comments on commit f182530

Please sign in to comment.