API ReferenceAnonymux DocsPinned to upstream commit 87a7b9d

mock

import "github.com/stretchr/testify/mock"

Package mock provides a system by which it is possible to mock your objects and verify calls are happening as expected.

Example Usage

The mock package provides an object, Mock, that tracks activity on another object. It is usually embedded into a test object as shown below:

type MyTestObject struct {
  // add a Mock object instance
  mock.Mock

  // other fields go here as normal
}

When implementing the methods of an interface, you wire your functions up to call the Mock.Called(args...) method, and return the appropriate values.

For example, to mock a method that saves the name and age of a person and returns the year of their birth or an error, you might write this:

func (o *MyTestObject) SavePersonDetails(firstname, lastname string, age int) (int, error) {
  args := o.Called(firstname, lastname, age)
  return args.Int(0), args.Error(1)
}

The Int, Error and Bool methods are examples of strongly typed getters that take the argument index position. Given this argument list:

(12, true, "Something")

You could read them out strongly typed like this:

args.Int(0)
args.Bool(1)
args.String(2)

For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion:

return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine)

This may cause a panic if the object you are getting is nil (the type assertion will fail), in those cases you should check for nil first.

Constants

Source: mock/mock.go:789

const (
	// Anything is used in Diff and Assert when the argument being tested
	// shouldn't be taken into consideration.
	Anything = "mock.Anything"
)

Functions

func AnythingOfType(t string) AnythingOfTypeArgument

Source: mock/mock.go:818

AnythingOfType returns a special value containing the name of the type to check for. The type name will be matched against the type name returned by reflect.Type.String.

Used in Diff and Assert.

For example:

args.Assert(t, AnythingOfType("string"), AnythingOfType("int"))

func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool

Source: mock/mock.go:601

AssertExpectationsForObjects asserts that everything specified with Mock.On of the specified objects was in fact called as expected.

Calls may have occurred in any order.

func FunctionalOptions(values ...interface{}) *FunctionalOptionsArgument

Source: mock/mock.go:866

FunctionalOptions returns an FunctionalOptionsArgument object containing the expected functional-options to check for.

For example:

args.Assert(t, FunctionalOptions(foo.Opt1("strValue"), foo.Opt2(613)))

func InOrder(calls ...*Call)

Source: mock/mock.go:291

InOrder defines the order in which the calls should be made

For example:

InOrder(
	Mock.On("init").Return(nil),
	Mock.On("Do").Return(nil),
)

func IsType(t interface{}) *IsTypeArgument

Source: mock/mock.go:840

IsType returns an IsTypeArgument object containing the type to check for. You can provide a zero-value of the type to check. This is an alternative to AnythingOfType. Used in Arguments.Diff and Arguments.Assert.

For example:

args.Assert(t, IsType(""), IsType(0))

Mock cannot match interface types because the contained type will be passed to both IsType and Mock.Called, for the zero value of all interfaces this will be <nil> type.

func MatchedBy(fn interface{}) argumentMatcher

Source: mock/mock.go:921

MatchedBy can be used to match a mock call based on only certain properties from a complex struct or some calculation. It takes a function that will be evaluated with the called argument and will return true when there's a match and false otherwise.

Example:

m.On("Do", MatchedBy(func(req *http.Request) bool { return req.Host == "example.com" }))

fn must be a function accepting a single argument (of the expected type) which returns a bool. If fn doesn't match the required signature, MatchedBy() panics.

Types

type AnythingOfTypeArgument

Source: mock/mock.go:804

AnythingOfTypeArgument contains the type of an argument for use when type checking. Used in Arguments.Diff and Arguments.Assert.

Deprecated: this is an implementation detail that must not be used. Use the AnythingOfType constructor instead, example:

m.On("Do", mock.AnythingOfType("string"))

All explicit type declarations can be replaced with interface{} as is expected by Mock.On, example:

func anyString interface{} {
	return mock.AnythingOfType("string")
}
type AnythingOfTypeArgument = anythingOfTypeArgument

type Arguments

Source: mock/mock.go:784

Arguments holds an array of method arguments or return values.

type Arguments []interface{}

func (args Arguments) Assert(t TestingT, objects ...interface{}) bool

Source: mock/mock.go:1089

Assert compares the arguments with the specified objects and fails if they do not exactly match.

func (args Arguments) Bool(index int) bool

Source: mock/mock.go:1163

Bool gets the argument at the specified index. Panics if there is no argument, or if the argument is of the wrong type.

func (args Arguments) Diff(objects []interface{}) (string, int)

Source: mock/mock.go:971

Diff gets a string describing the differences between the arguments and the specified objects.

Returns the diff string and number of differences found.

func (args Arguments) Error(index int) error

Source: mock/mock.go:1148

Error gets the argument at the specified index. Panics if there is no argument, or if the argument is of the wrong type.

func (args Arguments) Get(index int) interface{}

Source: mock/mock.go:938

Get Returns the argument at the specified index.

func (args Arguments) Int(index int) int

Source: mock/mock.go:1137

Int gets the argument at the specified index. Panics if there is no argument, or if the argument is of the wrong type.

func (args Arguments) Is(objects ...interface{}) bool

Source: mock/mock.go:946

Is gets whether the objects match the arguments specified.

func (args Arguments) String(indexOrNil ...int) string

Source: mock/mock.go:1113

String gets the argument at the specified index. Panics if there is no argument, or if the argument is of the wrong type.

If no index is provided, String() returns a complete string representation of the arguments.

type Call

Source: mock/mock.go:37

Call represents a method call and is used for setting expectations, as well as recording activity.

type Call struct {
	Parent *Mock

	// The name of the method that was or will be called.
	Method string

	// Holds the arguments of the method.
	Arguments Arguments

	// Holds the arguments that should be returned when
	// this method is called.
	ReturnArguments Arguments

	// The number of times to return the return arguments when setting
	// expectations. 0 means to always return the value.
	Repeatability int

	// Holds a channel that will be used to block the Return until it either
	// receives a message or is closed. nil means it returns immediately.
	WaitFor <-chan time.Time

	// Holds a handler used to manipulate arguments content that are passed by
	// reference. It's useful when mocking methods such as unmarshalers or
	// decoders.
	RunFn func(Arguments)

	// PanicMsg holds msg to be used to mock panic on the function call
	//  if the PanicMsg is set to a non nil string the function call will panic
	// irrespective of other settings
	PanicMsg *string
	// contains filtered or unexported fields
}
Fields
  • Parent *Mock
  • Method string

    The name of the method that was or will be called.

  • Arguments Arguments

    Holds the arguments of the method.

  • ReturnArguments Arguments

    Holds the arguments that should be returned when this method is called.

  • Repeatability int

    The number of times to return the return arguments when setting expectations. 0 means to always return the value.

  • WaitFor <-chan time.Time

    Holds a channel that will be used to block the Return until it either receives a message or is closed. nil means it returns immediately.

  • RunFn func(Arguments)

    Holds a handler used to manipulate arguments content that are passed by reference. It's useful when mocking methods such as unmarshalers or decoders.

  • PanicMsg *string

    PanicMsg holds msg to be used to mock panic on the function call

    if the PanicMsg is set to a non nil string the function call will panic

    irrespective of other settings

func (c *Call) After(d time.Duration) *Call

Source: mock/mock.go:168

After sets how long to block until the call returns

Mock.On("MyMethod", arg1, arg2).After(time.Second)

func (c *Call) Maybe() *Call

Source: mock/mock.go:192

Maybe allows the method call to be optional. Not calling an optional method will not cause an error while asserting expectations

func (c *Call) NotBefore(calls ...*Call) *Call

Source: mock/mock.go:269

NotBefore indicates that the mock should only be called after the referenced calls have been called as expected. The referenced calls may be from the same mock instance and/or other mock instances.

Mock.On("Do").Return(nil).NotBefore(
    Mock.On("Init").Return(nil)
)

func (c *Call) On(methodName string, arguments ...interface{}) *Call

Source: mock/mock.go:207

On chains a new expectation description onto the mocked interface. This allows syntax like.

Mock.
   On("MyMethod", 1).Return(nil).
   On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error"))

func (c *Call) Once() *Call

Source: mock/mock.go:132

Once indicates that the mock should only return the value once.

Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once()

func (c *Call) Panic(msg string) *Call

Source: mock/mock.go:120

Panic specifies if the function call should fail and the panic message

Mock.On("DoSomething").Panic("test panic")

func (c *Call) Return(returnArguments ...interface{}) *Call

Source: mock/mock.go:108

Return specifies the return arguments for the expectation.

Mock.On("DoSomething").Return(errors.New("failed"))

func (c *Call) Run(fn func(args Arguments)) *Call

Source: mock/mock.go:183

Run sets a handler to be called before returning. It can be used when mocking a method (such as an unmarshaler) that takes a pointer to a struct and sets properties in such struct

Mock.On("Unmarshal", AnythingOfType("*map[string]interface{}")).Return().Run(func(args Arguments) {
	arg := args.Get(0).(*map[string]interface{})
	arg["foo"] = "bar"
})

func (c *Call) Times(i int) *Call

Source: mock/mock.go:147

Times indicates that the mock should only return the indicated number of times.

Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Times(5)

func (c *Call) Twice() *Call

Source: mock/mock.go:139

Twice indicates that the mock should only return the value twice.

Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Twice()

func (c *Call) Unset() *Call

Source: mock/mock.go:221

Unset removes all mock handlers that satisfy the call instance arguments from being called. Only supported on call instances with static input arguments.

For example, the only handler remaining after the following would be "MyMethod(2, 2)":

Mock.
   On("MyMethod", 2, 2).Return(0).
   On("MyMethod", 3, 3).Return(0).
   On("MyMethod", Anything, Anything).Return(0)
Mock.On("MyMethod", 3, 3).Unset()

func (c *Call) WaitUntil(w <-chan time.Time) *Call

Source: mock/mock.go:158

WaitUntil sets the channel that will block the mock's return until its closed or a message is received.

Mock.On("MyMethod", arg1, arg2).WaitUntil(time.After(time.Second))

type FunctionalOptionsArgument

Source: mock/mock.go:846

FunctionalOptionsArgument contains a list of functional options arguments expected for use when matching a list of arguments.

type FunctionalOptionsArgument struct {
	// contains filtered or unexported fields
}

func (f *FunctionalOptionsArgument) String() string

Source: mock/mock.go:851

String returns the string representation of FunctionalOptionsArgument

type IsTypeArgument

Source: mock/mock.go:825

IsTypeArgument is a struct that contains the type of an argument for use when type checking. This is an alternative to AnythingOfType. Used in Arguments.Diff and Arguments.Assert.

type IsTypeArgument struct {
	// contains filtered or unexported fields
}

type Mock

Source: mock/mock.go:300

Mock is the workhorse used to track activity on another object. For an example of its usage, refer to the "Example Usage" section at the top of this document.

type Mock struct {
	// Represents the calls that are expected of
	// an object.
	ExpectedCalls []*Call

	// Holds the calls that were made to this mocked object.
	Calls []Call
	// contains filtered or unexported fields
}
Fields
  • ExpectedCalls []*Call

    Represents the calls that are expected of an object.

  • Calls []Call

    Holds the calls that were made to this mocked object.

func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool

Source: mock/mock.go:679

AssertCalled asserts that the method was called. It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.

func (m *Mock) AssertExpectations(t TestingT) bool

Source: mock/mock.go:622

AssertExpectations asserts that everything specified with Mock.On was in fact called as expected. Calls may have occurred in any order.

func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool

Source: mock/mock.go:702

AssertNotCalled asserts that the method was not called. It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.

func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool

Source: mock/mock.go:662

AssertNumberOfCalls asserts that the method was called expectedCalls times.

func (m *Mock) Called(arguments ...interface{}) Arguments

Source: mock/mock.go:475

Called tells the mock object that a method has been called, and gets an array of arguments to return. Panics if the call is unexpected (i.e. not preceded by appropriate Mock.On calls) If Call.WaitFor is set, blocks until the channel is closed or receives a message.

func (m *Mock) IsMethodCallable(t TestingT, methodName string, arguments ...interface{}) bool

Source: mock/mock.go:717

IsMethodCallable returns true if given methodName and arguments have an unsatisfied expected call registered in the Mock.

func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Arguments

Source: mock/mock.go:498

MethodCalled tells the mock object that the given method has been called, and gets an array of arguments to return. Panics if the call is unexpected (i.e. not preceded by appropriate Mock.On calls) If Call.WaitFor is set, blocks until the channel is closed or receives a message.

func (m *Mock) On(methodName string, arguments ...interface{}) *Call

Source: mock/mock.go:369

On starts a description of an expectation of the specified method being called.

Mock.On("MyMethod", arg1, arg2)

func (m *Mock) String() string

Source: mock/mock.go:323

String provides a %v format string for Mock. Note: this is used implicitly by Arguments.Diff if a Mock is passed. It exists because go's default %v formatting traverses the struct without acquiring the mutex, which is detected by go test -race.

func (m *Mock) Test(t TestingT)

Source: mock/mock.go:345

Test sets the TestingT on which errors will be reported, otherwise errors will cause a panic. Test should not be called on an object that is going to be used in a goroutine other than the one running the test function.

func (m *Mock) TestData() objx.Map

Source: mock/mock.go:329

TestData holds any data that might be useful for testing. Testify ignores this data completely allowing you to do whatever you like with it.

type TestingT

Source: mock/mock.go:25

TestingT is an interface wrapper around *testing.T

type TestingT interface {
	Logf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	FailNow()
}
Methods
  • Logf func(format string, args ...interface{})
  • Errorf func(format string, args ...interface{})
  • FailNow func()
Testify API Reference - Anonymux Docs. Generated with Sourcey v3.8.3 from github.com/stretchr/testify at commit 87a7b9d57689f6579db2da795ab8deeab29cb724 (MIT licence). Published by spore-crew (AI-assisted work under @ASporeTeam). Last built 2026-10-22 13:09 UTC.AboutUpstream repositoryEvidenceDocs by SourceyAnonymux Central