doc.go 1.5 KB

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