package gomock
import "github.com/golang/mock/gomock"
GoMock - a mock framework for Go.
Standard usage:
(1) Define an interface that you wish to mock. type MyInterface interface { SomeMethod(x int64, y string) } (2) Use mockgen to generate a mock from the interface. (3) Use the mock in a test: func TestMyThing(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockObj := something.NewMockMyInterface(mockCtrl) mockObj.EXPECT().SomeMethod(4, "blah") // pass mockObj to a real object and play with it. }
By default, expected calls are not enforced to run in any particular order. Call order dependency can be enforced by use of InOrder and/or Call.After. Call.After can create more varied call order dependencies, but InOrder is often more convenient.
The following examples create equivalent call order dependencies.
Example of using Call.After to chain expected call order:
firstCall := mockObj.EXPECT().SomeMethod(1, "first") secondCall := mockObj.EXPECT().SomeMethod(2, "second").After(firstCall) mockObj.EXPECT().SomeMethod(3, "third").After(secondCall)
Example of using InOrder to declare expected call order:
gomock.InOrder( mockObj.EXPECT().SomeMethod(1, "first"), mockObj.EXPECT().SomeMethod(2, "second"), mockObj.EXPECT().SomeMethod(3, "third"), )
TODO:
- Handle different argument/return types (e.g. ..., chan, map, interface).
Index ¶
- func InOrder(calls ...*Call)
- type Call
- func (c *Call) After(preReq *Call) *Call
- func (c *Call) AnyTimes() *Call
- func (c *Call) Do(f interface{}) *Call
- func (c *Call) DoAndReturn(f interface{}) *Call
- func (c *Call) MaxTimes(n int) *Call
- func (c *Call) MinTimes(n int) *Call
- func (c *Call) Return(rets ...interface{}) *Call
- func (c *Call) SetArg(n int, value interface{}) *Call
- func (c *Call) String() string
- func (c *Call) Times(n int) *Call
- type Controller
- func NewController(t TestReporter) *Controller
- func WithContext(ctx context.Context, t TestReporter) (*Controller, context.Context)
- func (ctrl *Controller) Call(receiver interface{}, method string, args ...interface{}) []interface{}
- func (ctrl *Controller) Finish()
- func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call
- func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call
- type Matcher
- func Any() Matcher
- func AssignableToTypeOf(x interface{}) Matcher
- func Eq(x interface{}) Matcher
- func Nil() Matcher
- func Not(x interface{}) Matcher
- type TestHelper
- type TestReporter
Functions ¶
func InOrder ¶
func InOrder(calls ...*Call)
InOrder declares that the given calls should occur in order.
Types ¶
type Call ¶
type Call struct {
// contains filtered or unexported fields
}
Call represents an expected call to a mock.
func (*Call) After ¶
After declares that the call may only match after preReq has been exhausted.
func (*Call) AnyTimes ¶
AnyTimes allows the expectation to be called 0 or more times
func (*Call) Do ¶
Do declares the action to run when the call is matched. The function's return values are ignored to retain backward compatibility. To use the return values call DoAndReturn. It takes an interface{} argument to support n-arity functions.
func (*Call) DoAndReturn ¶
DoAndReturn declares the action to run when the call is matched. The return values from this function are returned by the mocked function. It takes an interface{} argument to support n-arity functions.
func (*Call) MaxTimes ¶
MaxTimes limits the number of calls to n times. If AnyTimes or MinTimes have not been called, MaxTimes also sets the minimum number of calls to 0.
func (*Call) MinTimes ¶
MinTimes requires the call to occur at least n times. If AnyTimes or MaxTimes have not been called, MinTimes also sets the maximum number of calls to infinity.
func (*Call) Return ¶
Return declares the values to be returned by the mocked function call.
func (*Call) SetArg ¶
SetArg declares an action that will set the nth argument's value, indirected through a pointer. Or, in the case of a slice, SetArg will copy value's elements into the nth argument.
func (*Call) String ¶
func (*Call) Times ¶
Times declares the exact number of times a function call is expected to be executed.
type Controller ¶
type Controller struct { // T should only be called within a generated mock. It is not intended to // be used in user code and may be changed in future versions. T is the // TestReporter passed in when creating the Controller via NewController. // If the TestReporter does not implment a TestHelper it will be wrapped // with a nopTestHelper. T TestHelper // contains filtered or unexported fields }
A Controller represents the top-level control of a mock ecosystem. It defines the scope and lifetime of mock objects, as well as their expectations. It is safe to call Controller's methods from multiple goroutines.
func NewController ¶
func NewController(t TestReporter) *Controller
func WithContext ¶
func WithContext(ctx context.Context, t TestReporter) (*Controller, context.Context)
WithContext returns a new Controller and a Context, which is cancelled on any fatal failure.
func (*Controller) Call ¶
func (ctrl *Controller) Call(receiver interface{}, method string, args ...interface{}) []interface{}
func (*Controller) Finish ¶
func (ctrl *Controller) Finish()
func (*Controller) RecordCall ¶
func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call
func (*Controller) RecordCallWithMethodType ¶
func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call
type Matcher ¶
type Matcher interface { // Matches returns whether x is a match. Matches(x interface{}) bool // String describes what the matcher matches. String() string }
A Matcher is a representation of a class of values. It is used to represent the valid or expected arguments to a mocked method.
func Any ¶
func Any() Matcher
Constructors
func AssignableToTypeOf ¶
func AssignableToTypeOf(x interface{}) Matcher
AssignableToTypeOf is a Matcher that matches if the parameter to the mock function is assignable to the type of the parameter to this function.
Example usage:
dbMock.EXPECT(). Insert(gomock.AssignableToTypeOf(&EmployeeRecord{})). Return(errors.New("DB error"))
func Eq ¶
func Eq(x interface{}) Matcher
func Nil ¶
func Nil() Matcher
func Not ¶
func Not(x interface{}) Matcher
type TestHelper ¶
type TestHelper interface { TestReporter Helper() }
TestHelper is a TestReporter that has the Helper method. It is satisfied by the standard library's *testing.T.
type TestReporter ¶
type TestReporter interface { Errorf(format string, args ...interface{}) Fatalf(format string, args ...interface{}) }
A TestReporter is something that can be used to report test failures. It is satisfied by the standard library's *testing.T.
Source Files ¶
call.go callset.go controller.go matchers.go
Directories ¶
Path | Synopsis |
---|---|
gomock/internal |
- Version
- v1.2.0
- Published
- Dec 8, 2018
- Platform
- windows/amd64
- Imports
- 8 packages
- Last checked
- 44 seconds ago –
Tools for package owners.