Documentation ¶
Overview ¶
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) AtLeastTimes(n int) *Call
- func (c *Call) AtMostTimes(n int) *Call
- func (c *Call) Do(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
- type Matcher
- type TestReporter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Call ¶
type Call struct {
// contains filtered or unexported fields
}
Call represents an expected call to a mock.
func (*Call) AtLeastTimes ¶
AtLeastTimes sets the minimum number of times an expectation must be called, and sets the upper boundary to infinity. AtLeastTimes is a convenience method, equivalent to mock.EXPECT().Call().MinTimes(n).MaxTimes(1e8)
func (*Call) AtMostTimes ¶
AtMostTimes sets the maximum number of times an expectation may be called, and sets the minimum number to 0. AtMostTimes is a convenience method, equivalent to mock.EXPECT().Call().MinTimes(0).MaxTimes(n)
func (*Call) Do ¶
Do declares the action to run when the call is matched. It takes an interface{} argument to support n-arity functions.
func (*Call) MaxTimes ¶
MaxTimes sets the maximum number of times an expectation may be called. It is expected to be used along with MinTimes
func (*Call) MinTimes ¶
MinTimes sets the minimum number of times an expectation must be called. It is expected to be used along with MaxTimes
type Controller ¶
type Controller struct {
// 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 (*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
type Matcher ¶
type Matcher interface { // Matches returns whether y 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.
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.