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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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