Documentation ¶
Overview ¶
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.Mock.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.
Index ¶
- Constants
- func AssertExpectationsForObjects(t *testing.T, testObjects ...interface{}) bool
- type AnythingOfTypeArgument
- type Arguments
- func (args Arguments) Assert(t *testing.T, objects ...interface{}) bool
- func (args Arguments) Bool(index int) bool
- func (args Arguments) Diff(objects []interface{}) (string, int)
- func (args Arguments) Error(index int) error
- func (args Arguments) Get(index int) interface{}
- func (args Arguments) Int(index int) int
- func (args Arguments) Is(objects ...interface{}) bool
- func (args Arguments) String(indexOrNil ...int) string
- type Call
- type Mock
- func (m *Mock) AssertCalled(t *testing.T, methodName string, arguments ...interface{}) bool
- func (m *Mock) AssertExpectations(t *testing.T) bool
- func (m *Mock) AssertNotCalled(t *testing.T, methodName string, arguments ...interface{}) bool
- func (m *Mock) AssertNumberOfCalls(t *testing.T, methodName string, expectedCalls int) bool
- func (m *Mock) Called(arguments ...interface{}) Arguments
- func (m *Mock) On(methodName string, arguments ...interface{}) *Mock
- func (m *Mock) Return(returnArguments ...interface{}) *Mock
- func (m *Mock) TestData() objx.Map
Constants ¶
const ( // The "any" argument. Used in Diff and Assert when // the argument being tested shouldn't be taken into consideration. Anything string = "mock.Anything" )
Variables ¶
This section is empty.
Functions ¶
func AssertExpectationsForObjects ¶
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.
Types ¶
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 ¶
Assert compares the arguments with the specified objects and fails if they do not exactly match.
func (Arguments) 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 ¶
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 ¶
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) Int ¶
Int gets the argument at the specified index. Panics if there is no argument, or if the argument is of the wrong type.
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 }
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) AssertCalled ¶
AssertCalled asserts that the method was called.
func (*Mock) AssertExpectations ¶
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 ¶
AssertNotCalled asserts that the method was not called.
func (*Mock) AssertNumberOfCalls ¶
AssertNumberOfCalls asserts that the method was called expectedCalls times.
func (*Mock) Called ¶
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)
func (*Mock) On ¶
On starts a description of an expectation of the specified method being called.
Mock.On("MyMethod", arg1, arg2)