Documentation ¶
Overview ¶
Package mock provides a mocking framework for Go.
Index ¶
- func AssertVerifyMocks(t HasError, mocks ...HasVerify)
- func VerifyMocks(mocks ...HasVerify) (bool, error)
- type AnyIfType
- type AnyType
- type AnythingOfType
- type HasError
- type HasVerify
- type Mock
- type MockCountCheckType
- type MockFunction
- func (f *MockFunction) AtLeast(a int) *MockFunction
- func (f *MockFunction) AtMost(a int) *MockFunction
- func (f *MockFunction) Between(a, b int) *MockFunction
- func (f *MockFunction) Call(call interface{}) *MockFunction
- func (f *MockFunction) Panic(v interface{}) *MockFunction
- func (f *MockFunction) Return(v ...interface{}) *MockFunction
- func (f *MockFunction) ReturnToArgument(n int, v interface{}) *MockFunction
- func (f *MockFunction) Timeout(d time.Duration) *MockFunction
- func (f *MockFunction) Times(a int) *MockFunction
- type MockResult
- func (r *MockResult) Bool(i int) bool
- func (r *MockResult) Byte(i int) byte
- func (r *MockResult) Bytes(i int) []byte
- func (r *MockResult) Contains(i int) bool
- func (r *MockResult) Error(i int) error
- func (r *MockResult) Float32(i int) float32
- func (r *MockResult) Float64(i int) float64
- func (r *MockResult) Get(i int) interface{}
- func (r *MockResult) GetType(i int, ii interface{}) interface{}
- func (r *MockResult) Int(i int) int
- func (r *MockResult) Int16(i int) int16
- func (r *MockResult) Int32(i int) int32
- func (r *MockResult) Int64(i int) int64
- func (r *MockResult) Int8(i int) int8
- func (r *MockResult) String(i int) string
- type MockReturnToArgument
- type RestType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertVerifyMocks ¶
Fail the test if any of the mocks fail verification
func VerifyMocks ¶
VerifyMocks verifies a list of mocks, and returns the first error, if any.
Types ¶
type AnyIfType ¶
type AnyIfType func(interface{}) bool
AnyIfType defines the type used as an argument that satisfies a condition.
type AnyType ¶
type AnyType string
AnyType defines the type used as a replacement for any kind of argument in the stub configuration.
const (
Any AnyType = "mock.any"
)
Any is the constant that should be used to represent a AnyType.
Example:
mock.When("MyMethod", mock.Any, mock.Any).Return(0)
type AnythingOfType ¶
type AnythingOfType string
AnythingOfType defines the type used as a replacement for an argument of a specific type in the stub configuration.
func AnyOfType ¶
func AnyOfType(t string) AnythingOfType
AnyOfType is a helper to define AnythingOfType arguments.
Example:
mock.When("MyMethod", mock.AnyOfType("int"), mock.AnyOfType("string")).Return(0)
type HasError ¶
type HasError interface {
Error(...interface{})
}
Used to represent a test we can fail, without importing the testing package Importing "testing" in a file not named *_test.go results in tons of test.* flags being added to any compiled binary including this package
type Mock ¶
type Mock struct { Functions []*MockFunction // contains filtered or unexported fields }
Mock should be embedded in the struct that we want to act as a Mock.
Example:
type MyClient struct { mock.Mock }
func (*Mock) Called ¶
func (m *Mock) Called(arguments ...interface{}) *MockResult
Called is the function used in the mocks to replace the actual task.
Example:
func (m *MyClient) Request(url string) (int, string, error) { r := m.Called(url) return r.Int(0), r.String(1), r.Error(2) }
func (*Mock) When ¶
func (m *Mock) When(name string, arguments ...interface{}) *MockFunction
When defines an stub of one method with some specific arguments. It returns a *MockFunction that can be configured with Return, ReturnToArgument, Panic, ...
type MockCountCheckType ¶
type MockCountCheckType int
const ( NONE MockCountCheckType = iota TIMES AT_LEAST AT_MOST BETWEEN )
type MockFunction ¶
type MockFunction struct { Name string Arguments []interface{} ReturnValues []interface{} ReturnToArguments []MockReturnToArgument PanicValue interface{} // contains filtered or unexported fields }
MockFunction is the struct used to store the properties of a method stub.
func (*MockFunction) AtLeast ¶
func (f *MockFunction) AtLeast(a int) *MockFunction
AtLeast defines the number of times that a *MockFunction must be at least called. This is verified if mock.Verify is called.
func (*MockFunction) AtMost ¶
func (f *MockFunction) AtMost(a int) *MockFunction
AtMost defines the number of times that a *MockFunction must be at most called. This is verified if mock.Verify is called.
func (*MockFunction) Between ¶
func (f *MockFunction) Between(a, b int) *MockFunction
Between defines a range of times that a *MockFunction must be called. This is verified if mock.Verify is called.
func (*MockFunction) Call ¶
func (f *MockFunction) Call(call interface{}) *MockFunction
Call executes a function passed as an argument using the arguments pased to the stub. If the function returns any output parameters they will be used as a return arguments when the stub is called. If the call argument is not a function it will panic when the stub is executed.
Example:
mock.When("MyMethod", mock.Any, mock.Any).Call(func(a int, b int) int { return a+b })
func (*MockFunction) Panic ¶
func (f *MockFunction) Panic(v interface{}) *MockFunction
Panic defines a panic for a specific *MockFunction.
func (*MockFunction) Return ¶
func (f *MockFunction) Return(v ...interface{}) *MockFunction
Return defines the return values of a *MockFunction.
func (*MockFunction) ReturnToArgument ¶
func (f *MockFunction) ReturnToArgument(n int, v interface{}) *MockFunction
ReturnToArgument defines the values returned to a specific argument of a *MockFunction.
func (*MockFunction) Timeout ¶
func (f *MockFunction) Timeout(d time.Duration) *MockFunction
Timeout defines a timeout to sleep before returning the value of a function.
func (*MockFunction) Times ¶
func (f *MockFunction) Times(a int) *MockFunction
Times defines how many times a *MockFunction must be called. This is verified if mock.Verify is called.
type MockResult ¶
type MockResult struct {
Result []interface{}
}
MockResult struct is used to store the return arguments of a method stub.
func (*MockResult) Bool ¶
func (r *MockResult) Bool(i int) bool
Bool returns a specific return parameter as a bool. If a result has not been set, it returns false.
func (*MockResult) Byte ¶
func (r *MockResult) Byte(i int) byte
Byte returns a specific return parameter as a byte. If a result has not been set, it returns 0.
func (*MockResult) Bytes ¶
func (r *MockResult) Bytes(i int) []byte
Bytes returns a specific return parameter as a []byte. If a result has not been set, it returns nil.
func (*MockResult) Contains ¶
func (r *MockResult) Contains(i int) bool
Contains returns true if the results have the index i, false otherwise.
func (*MockResult) Error ¶
func (r *MockResult) Error(i int) error
Error returns a specific return parameter as an error. If a result has not been set, it returns nil.
func (*MockResult) Float32 ¶
func (r *MockResult) Float32(i int) float32
Float32 returns a specific return parameter as a float32. If a result has not been set, it returns 0.
func (*MockResult) Float64 ¶
func (r *MockResult) Float64(i int) float64
Float64 returns a specific return parameter as a float64. If a result has not been set, it returns 0.
func (*MockResult) Get ¶
func (r *MockResult) Get(i int) interface{}
Get returns a specific return parameter. If a result has not been set, it returns nil,
func (*MockResult) GetType ¶
func (r *MockResult) GetType(i int, ii interface{}) interface{}
GetType returns a specific return parameter with the same type of the second argument. A nil version of the type can be casted without causing a panic.
func (*MockResult) Int ¶
func (r *MockResult) Int(i int) int
Int returns a specific return parameter as an int. If a result has not been set, it returns 0.
func (*MockResult) Int16 ¶
func (r *MockResult) Int16(i int) int16
Int16 returns a specific return parameter as an int16. If a result has not been set, it returns 0.
func (*MockResult) Int32 ¶
func (r *MockResult) Int32(i int) int32
Int32 returns a specific return parameter as an int32. If a result has not been set, it returns 0.
func (*MockResult) Int64 ¶
func (r *MockResult) Int64(i int) int64
Int64 returns a specific return parameter as an int64. If a result has not been set, it returns 0.
func (*MockResult) Int8 ¶
func (r *MockResult) Int8(i int) int8
Int8 returns a specific return parameter as an int8. If a result has not been set, it returns 0.
func (*MockResult) String ¶
func (r *MockResult) String(i int) string
String returns a specific return parameter as a string. If a result has not been set, it returns "".
type MockReturnToArgument ¶
type MockReturnToArgument struct { Argument int Value interface{} }
MockReturnToArgument defines the function arguments used as return parameters.