...

Source file src/github.com/stretchr/testify/mock/doc.go

Documentation: github.com/stretchr/testify/mock

     1  // Package mock provides a system by which it is possible to mock your objects
     2  // and verify calls are happening as expected.
     3  //
     4  // # Example Usage
     5  //
     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:
     8  //
     9  //	type MyTestObject struct {
    10  //	  // add a Mock object instance
    11  //	  mock.Mock
    12  //
    13  //	  // other fields go here as normal
    14  //	}
    15  //
    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.
    18  //
    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:
    21  //
    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)
    25  //	}
    26  //
    27  // The Int, Error and Bool methods are examples of strongly typed getters that take the argument
    28  // index position. Given this argument list:
    29  //
    30  //	(12, true, "Something")
    31  //
    32  // You could read them out strongly typed like this:
    33  //
    34  //	args.Int(0)
    35  //	args.Bool(1)
    36  //	args.String(2)
    37  //
    38  // For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion:
    39  //
    40  //	return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine)
    41  //
    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.
    44  package mock
    45  

View as plain text