-
Notifications
You must be signed in to change notification settings - Fork 80
/
mocks.go
29 lines (26 loc) · 936 Bytes
/
mocks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package testutils
import "github.com/stretchr/testify/mock"
// ReplaceMock allows you to mock out a method,
// overriding any previous expectations for that same method.
//
// This is useful if you want to setup a generic stub object
// to reuse among multiple tests, but then tweak expectations
// for some of the tests.
//
// Note that you need to pass the underlying mock object to this method,
// as `mock.Mock` does not expose any useful interfaces
// eg.
//
// ReplaceMock(&myMock.Mock, "DoAThing", mock.Anything).Return(nil)
func ReplaceMock(m *mock.Mock, methodName string, arguments ...interface{}) *mock.Call {
// Recreated the list of expected calls,
// excluding calls for this method
var expectedCalls []*mock.Call
for _, call := range m.ExpectedCalls {
if call.Method != methodName {
expectedCalls = append(expectedCalls, call)
}
}
m.ExpectedCalls = expectedCalls
return m.On(methodName, arguments...)
}