Documentation
¶
Overview ¶
Package mock contains the basic collection of functions and types for controlling mocks and mock request/response setup. It is part of the public interface and starting to get stable, however, we are still experimenting to optimize the interface and the user experience.
Index ¶
- Variables
- func Chain(fncalls ...func(*Mocks) any) func(*Mocks) any
- func Detach(mode DetachMode, fncall func(*Mocks) any) func(*Mocks) any
- func Get[T any](mocks *Mocks, creator func(*Controller) *T) *T
- func GetSubSlice[T any](from, to int, calls []T) any
- func NewErrDetachMode(mode DetachMode) error
- func NewErrDetachNotAllowed(mode DetachMode) error
- func NewErrNoCall(call any) error
- func Parallel(fncalls ...func(*Mocks) any) func(*Mocks) any
- func Setup(fncalls ...func(*Mocks) any) func(*Mocks) any
- func Sub(from, to int, fncall func(*Mocks) any) func(*Mocks) any
- type Call
- type Controller
- type DetachMode
- type Mocks
- func (mocks *Mocks) Add(delta int) int
- func (mocks *Mocks) Call(fn any, call func(...any) []any) any
- func (mocks *Mocks) Do(fn any, args ...any) any
- func (mocks *Mocks) Done()
- func (mocks *Mocks) Expect(fncalls SetupFunc) *Mocks
- func (mocks *Mocks) Get(creator reflect.Value) any
- func (mocks *Mocks) GetArg(key any) any
- func (mocks *Mocks) Panic(fn any, reason any) any
- func (mocks *Mocks) Return(fn any, args ...any) any
- func (mocks *Mocks) SetArg(key any, value any) *Mocks
- func (mocks *Mocks) SetArgs(args map[any]any) *Mocks
- func (mocks *Mocks) Times(num int) int
- func (mocks *Mocks) Wait()
- type SetupFunc
Constants ¶
This section is empty.
Variables ¶
var ( // Error type for unsupported type errors. ErrTypeNotSupported = errors.New("type not supported") // Error type for unsupported mode errors. ErrModeNotSupprted = errors.New("mode not supported") )
Functions ¶
func Chain ¶
Chain creates a single chain of mock calls that is validated by `gomock`. If the execution order deviates from the order defined in the chain, the test validation fails. The method returns the full mock calls tree to allow chaining with other ordered setup method.
func Detach ¶
func Detach(mode DetachMode, fncall func(*Mocks) any) func(*Mocks) any
Detach detach given mock call setup using given detach mode. It is possible to detach the mock call from the preceding mock calls (`Head`), from the succeeding mock calls (`Tail`), or from both as used in `Setup`.
func Get ¶
func Get[T any](mocks *Mocks, creator func(*Controller) *T) *T
Get resolves the actual mock from the mock handler by providing the constructor function generated by `gomock` to create a new mock.
func GetSubSlice ¶
GetSubSlice returns the sub slice of mock calls starting at index `from` up to index `to` including. A negative value is used to calculate an index from the end of the slice. If the index `from` is after the index `to`, the indexes are automatically switched.
func NewErrDetachMode ¶
func NewErrDetachMode(mode DetachMode) error
NewErrDetachMode creates an error that the given detach mode is not supported.
func NewErrDetachNotAllowed ¶
func NewErrDetachNotAllowed(mode DetachMode) error
NewErrDetachNotAllowed creates an error that the detach mode is not supported.
func NewErrNoCall ¶
NewErrNoCall creates an error with given call type to panic on incorrect call type.
func Parallel ¶
Parallel creates a set of parallel set of mock calls that is validated by `gomock`. While the parallel setup provides some freedom, this still defines constraints with respect to parent and child setup methods, e.g. when setting up parallel chains in a chain, each parallel chains needs to follow the last mock call and finish before the following mock call.
If the execution order deviates from the order defined by the parallel context, the test validation fails. The method returns the full set of mock calls to allow combining them with other ordered setup methods.
func Setup ¶
Setup creates only a lazily ordered set of mock calls that is detached from the parent setup by returning no calls for chaining. The mock calls created by the setup are only validated in so far in relation to each other, that `gomock` delivers results for the same mock call receiver in the order provided during setup.
func Sub ¶
Sub returns the sub slice of mock calls starting at index `from` up to index `to` including. A negative value is used to calculate an index from the end of the slice. If the index of `from` is higher as the index `to`, the indexes are automatically switched. The returned sub slice of mock calls keeps its original semantic.
Types ¶
type DetachMode ¶
type DetachMode int
DetachMode defines the mode for detaching mock calls.
const ( // None mode to not detach mode. None DetachMode = 0 // Head mode to detach head, i.e. do not order mock calls after predecessor // mock calls provided via context. Head DetachMode = 1 // Tail mode to detach tail, i.e. do not order mock calls before successor // mock calls provided via context. Tail DetachMode = 2 // Both mode to detach tail and head, i.e. do neither order mock calls after // predecessor nor before successor provided via context. Both DetachMode = 3 )
func (DetachMode) String ¶
func (m DetachMode) String() string
String return string representation of detach mode.
type Mocks ¶
type Mocks struct { // The mock controller used. Ctrl *Controller // contains filtered or unexported fields }
Mocks common mock handler.
func NewMocks ¶
func NewMocks(t gomock.TestReporter) *Mocks
NewMocks creates a new mock handler using given test reporter, e.g. *testing.T, or [test.Test].
func (*Mocks) Add ¶
Add adds the given delta on the wait group to register the expected or notify the consumed mock calls. This method implements the sync.WaitGroup interface to support testing of detached *goroutines* in an isolated [test](../test) environment.
**Note:** Usually call expectation setup is completely handled via `Call`, `Do`, `Return`, and `Panic`. Use this method only for synchronizing tests *goroutines*.
func (*Mocks) Call ¶
Call is a convenience method to setup a call back function for gomock.Do and gomock.DoAndReturn. Using this method signals an expected mock call during setup as well as a consumed mock call when executing the given call back function. The function is supplied with the regular call parameters and expected to return the mock result - if required, as gomock.Do ignores arguments.
**Note:** Call registers exactly one expected call automatically.
func (*Mocks) Do ¶
Do is a convenience method to setup a call back function for gomock.Do or gomock.DoAndReturn. Using this method signals an expected mock call during setup as well as a consumed mock call when executing the given call back function returning the given optional arguments as mock result - if necessary, as gomock.Do ignores arguments.
**Note:** Do registers exactly one expected call automatically.
func (*Mocks) Done ¶
func (mocks *Mocks) Done()
Done removes exactly one expected mock call from the wait group to notify a consumed mock call. This method implements the sync.WaitGroup interface to support testing of detached `go-routines` in an isolated [test](../test) environment.
**Note:** Usually call expectation setup is completely handled via `Call`, `Do`, `Return`, and `Panic`. Use this method only for synchronizing tests *goroutines*.
func (*Mocks) Get ¶
Get resolves the singleton mock from the mock handler by providing the reflection value of the constructor function generated by gomock to create a new mock. The mock is only created once and stored in an internal creator function to mock map.
func (*Mocks) GetArg ¶
GetArg gets the mock argument value for the given argument key. This can be used to access a common test arguments from a mock call.
func (*Mocks) Panic ¶
Panic is a convenience method to setup a call back function that panics with given reason for gomock.Do or gomock.DoAndReturn. Using this method signals an expected mock call during setup as well as a consumed mock call when executing the given call back function.
**Note:** Return registers exactly one expected call automatically.
func (*Mocks) Return ¶
Return is a convenience method to setup a call back function for gomock.Do or gomock.DoAndReturn. Using this method signals an expected mock call during setup as well as a consumed mock call when executing the given call back function returning the given optional arguments as mock result - if necessary, as gomock.Do ignores arguments.
**Note:** Return registers exactly one expected call automatically.
func (*Mocks) SetArg ¶
SetArg sets the given mock argument value for the given argument key. This can be used to pass a common test arguments to mock calls.
func (*Mocks) SetArgs ¶
SetArgs sets the given mock argument values for the given argument keys. This can be used to pass a set of common test arguments to mock calls.
func (*Mocks) Times ¶
Times is creating the expectation that exactly the given number of mock call are consumed. This call is supposed to be used as input for gomock.Times in combination with Call, [Do], [Return], and [Panic]. Setting up [Times] is considering that these methods add one expected call by reducing the registration by one.
func (*Mocks) Wait ¶
func (mocks *Mocks) Wait()
Wait waits for all mock calls registered via Call, [Do], [Return], [Panic], and [Times] to be consumed before testing can continue. This method implements the sync.WaitGroup interface to support testing of detached *goroutines* in an isolated [test](../test) environment.