v0.1.6 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2015 License: MIT Imports: 8 Imported by: 0



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

  // 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:


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.



View Source
const (
	// The "any" argument.  Used in Diff and Assert when
	// the argument being tested shouldn't be taken into consideration.
	Anything string = "mock.Anything"


This section is empty.


func AssertExpectationsForObjects

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

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

Calls may have occurred in any order.


type AnythingOfTypeArgument

type AnythingOfTypeArgument string

AnythingOfTypeArgument is a string that contains the type of an argument for use when type checking. Used in Diff and Assert.

func AnythingOfType

func AnythingOfType(t string) AnythingOfTypeArgument

AnythingOfType returns an AnythingOfTypeArgument object containing the name of the type to check for. Used in Diff and Assert.

For example:

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

type Arguments

type Arguments []interface{}

Arguments holds an array of method arguments or return values.

func (Arguments) Assert

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

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

func (Arguments) Bool

func (args Arguments) Bool(index int) bool

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

func (Arguments) Diff

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

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

Returns the diff string and number of differences found.

func (Arguments) Error

func (args Arguments) Error(index int) error

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

func (Arguments) Get

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

Get Returns the argument at the specified index.

func (Arguments) Int

func (args Arguments) Int(index int) int

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

func (Arguments) Is

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

Is gets whether the objects match the arguments specified.

func (Arguments) String

func (args Arguments) String(indexOrNil string

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

type Call struct {

	// 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
	// recieves 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.
	Run func(Arguments)

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

type Mock

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

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.

func (*Mock) After

func (m *Mock) After(d time.Duration) *Mock

After sets how long to block until the call returns

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

func (*Mock) AssertCalled

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

AssertCalled asserts that the method was called.

func (*Mock) AssertExpectations

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

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

func (*Mock) AssertNotCalled

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

AssertNotCalled asserts that the method was not called.

func (*Mock) AssertNumberOfCalls

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

AssertNumberOfCalls asserts that the method was called expectedCalls times.

func (*Mock) Called

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

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 preceeded by appropriate .On .Return() calls) If Call.WaitFor is set, blocks until the channel is closed or receives a message.

func (*Mock) On

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

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

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

func (*Mock) Once

func (m *Mock) Once()

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

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

func (*Mock) Return

func (m *Mock) Return(returnArguments ...interface{}) *Mock

Return finishes a description of an expectation of the method (and arguments) specified in the most recent On method call.

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

func (*Mock) Run

func (m *Mock) Run(fn func(Arguments)) *Mock

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

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

func (*Mock) TestData

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

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

func (*Mock) Times

func (m *Mock) Times(i int)

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

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

func (*Mock) Twice

func (m *Mock) Twice()

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

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

func (*Mock) WaitUntil

func (m *Mock) WaitUntil(w <-chan time.Time) *Mock

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 TestingT

type TestingT interface {
	Logf(format string, args ...interface{})
	Errorf(format string, args ...interface{})

TestingT is an interface wrapper around *testing.T

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL