1 // Package mock provides a system by which it is possible to mock your objects
2 // and verify calls are happening as expected.
6 // The mock package provides an object, Mock, that tracks activity on another object. It is usually
7 // embedded into a test object as shown below:
9 // type MyTestObject struct {
10 // // add a Mock object instance
13 // // other fields go here as normal
16 // When implementing the methods of an interface, you wire your functions up
17 // to call the Mock.Called(args...) method, and return the appropriate values.
19 // For example, to mock a method that saves the name and age of a person and returns
20 // the year of their birth or an error, you might write this:
22 // func (o *MyTestObject) SavePersonDetails(firstname, lastname string, age int) (int, error) {
23 // args := o.Called(firstname, lastname, age)
24 // return args.Int(0), args.Error(1)
27 // The Int, Error and Bool methods are examples of strongly typed getters that take the argument
28 // index position. Given this argument list:
30 // (12, true, "Something")
32 // You could read them out strongly typed like this:
38 // For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion:
40 // return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine)
42 // This may cause a panic if the object you are getting is nil (the type assertion will fail), in those
43 // cases you should check for nil first.