gomockext is a library which adds extensions useful for simplifying testing using gomock
To get the latest released version use:
go get github.com/garvincasimir/gomockext@v1.0.0
You can create a custom matcher to meet more specific conditions by calling the gomockext.Match
function.
// StartsWith returns a matcher which checks if a string starts with a specific prefix
func StartsWith(prefix, message string) mockext.CustomMatcher {
return mockext.Match(func(got any) bool {
if str, ok := got.(string); ok {
return strings.HasPrefix(str, prefix)
}
return false
}, message)
}
func TestArg1StringStartsWith(t *testing.T) {
ctrl := gomock.NewController(t)
fooBar := NewMockFooBar(ctrl)
fooBar.EXPECT().WithString(StartsWith("Foo", "Arg should start with Foo")).Times(1)
fooBar.WithString("FooBar")
}
The library comes with several built in matchers. Please take a look at the examples folder for usage examples.
// StartsWith returns a matcher which checks if a string starts with a specific prefix
func TestArg1StringStartsWith(t *testing.T) {
ctrl := gomock.NewController(t)
fooBar := NewMockFooBar(ctrl)
fooBar.EXPECT().WithString(stringext.StartsWith("Foo", "Arg should start with Foo")).Times(1)
fooBar.WithString("FooBar")
}
Please feel free to submit bugs, pull requests and suggestions for improving the library. The repo is configured to create a custom Codespace with everything you need to start contributing