Documentation
¶
Overview ¶
Package mmock - Go package extension for https://github.com/stretchr/testify
Provides additional methods on testify.mock that allow methods to be specified by func rather than string name ¶
Which alleviates errors that can be introduced during refactoring
Index ¶
- func As[T any](args mock.Arguments, index int) T
- func As1[T1 any](args mock.Arguments) T1
- func As2[T1 any, T2 any](args mock.Arguments) (T1, T2)
- func As3[T1 any, T2 any, T3 any](args mock.Arguments) (T1, T2, T3)
- func As4[T1 any, T2 any, T3 any, T4 any](args mock.Arguments) (T1, T2, T3, T4)
- func MockGenerate[T any](pkg string) ([]byte, error)
- func MockGenerateFile[T any](pkg string, f *os.File) error
- func NewMock[T any]() *T
- func NewMockOf[T any, I any]() *T
- func NewSpyMockOf[T any, I any](wrapped I) *T
- type MockMethods
- func (mm *MockMethods) AssertMethodCalled(t *testing.T, method any, arguments ...any) bool
- func (mm *MockMethods) AssertMethodNotCalled(t *testing.T, method any, arguments ...any) bool
- func (mm *MockMethods) AssertNumberOfMethodCalls(t *testing.T, method any, expectedCalls int) bool
- func (mm *MockMethods) Called(arguments ...interface{}) mock.Arguments
- func (mm *MockMethods) MethodCalled(methodName string, arguments ...interface{}) (result mock.Arguments)
- func (mm *MockMethods) OnAllMethods(errs bool)
- func (mm *MockMethods) OnMethod(method any, arguments ...any) *mock.Call
- func (mm *MockMethods) SetSpyOf(wrapped any)
- type Spying
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MockGenerate ¶
MockGenerate generates Go code with a mock for interface type T
The pkg arg is the package name (or package path) for the code - if this is an empty string then the package of the interface type T is used
Caveat emptor! The generated code is designed to save you work but is not 100% guaranteed to produce compilable code (it can get confused with convoluted or conflicting package names) and may sometimes require manual intervention.
func MockGenerateFile ¶
MockGenerateFile generates a Go file with a mock for interface type T
The pkg arg is the package name (or package path) for the code - if this is an empty string then the package of the interface type T is used
Caveat emptor! The generated code is designed to save you work but is not 100% guaranteed to produce compilable code (it can get confused with convoluted or conflicting package names) and may sometimes require manual intervention.
func NewMock ¶
func NewMock[T any]() *T
NewMock creates a new mock of a specified type
Note: It ensures that the type implements mocks.MockMethods (and panics if it does not!)
Example usage:
type MockedSomething struct { mocks.MockMethods } myMock := NewMock[MockedSomething]()
func NewMockOf ¶
NewMockOf creates a new mock of a specified type
same as NewMock except that it also checks that the specified type implements the interface specified by the second generic arg (and panics if it does not!)
Example usage:
type my interface { SomeMethod() } type MockedSomething struct { mocks.MockMethods } func (m *MockedSomething) SomeMethod() { } myMock := NewMockOf[MockedSomething, my]()
func NewSpyMockOf ¶ added in v1.1.0
NewSpyMockOf creates a new spy mock of a specified type and provides the underling wrapped implementation
The wrapped arg is implementation to be spied on - any methods that are called on the mock but have not been expected (by using On or OnMethod) will call this underlying - but you can still assert that the method has been called
Types ¶
type MockMethods ¶
MockMethods is the replacement for mock.Mock
func (*MockMethods) AssertMethodCalled ¶
AssertMethodCalled is the same as Mock.AssertCalled() (https://pkg.go.dev/github.com/stretchr/testify/mock#Mock.AssertCalled)
Except the method can be specified by func pointer or name ¶
Also, if the number of arguments specified is less than the expected args of the method then the arguments is padded with mock.Anything
func (*MockMethods) AssertMethodNotCalled ¶
AssertMethodNotCalled is the same as Mock.AssertNotCalled() (https://pkg.go.dev/github.com/stretchr/testify/mock#Mock.AssertNotCalled)
Except the method can be specified by func pointer or name ¶
Also, if the number of arguments specified is less than the expected args of the method then the arguments is padded with mock.Anything
func (*MockMethods) AssertNumberOfMethodCalls ¶
AssertNumberOfMethodCalls is the same as Mock.AssertNumberOfCalls() (https://pkg.go.dev/github.com/stretchr/testify/mock#Mock.AssertNumberOfCalls)
Except the method can be specified by func pointer or name
func (*MockMethods) Called ¶ added in v1.1.0
func (mm *MockMethods) Called(arguments ...interface{}) mock.Arguments
func (*MockMethods) MethodCalled ¶ added in v1.1.0
func (mm *MockMethods) MethodCalled(methodName string, arguments ...interface{}) (result mock.Arguments)
func (*MockMethods) OnAllMethods ¶
func (mm *MockMethods) OnAllMethods(errs bool)
OnAllMethods setups expected calls on every method of the mock
Use the errs arg to specify that methods that return an error should return an error when called
func (*MockMethods) OnMethod ¶
func (mm *MockMethods) OnMethod(method any, arguments ...any) *mock.Call
OnMethod is the same as Mock.On() (https://pkg.go.dev/github.com/stretchr/testify/mock#Call.On)
Except the method can be specified by func pointer or name
func (*MockMethods) SetSpyOf ¶ added in v1.1.0
func (mm *MockMethods) SetSpyOf(wrapped any)
type Spying ¶ added in v1.1.0
type Spying interface { // SetSpyOf sets the mock to be a spy mock // // The wrapped arg is implementation to be spied on - any methods that are called on the mock // but have not been expected (by using On or OnMethod) will call this underlying - but you can still assert // that the method has been called SetSpyOf(wrapped any) }