You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. gomock [![Build Status][travis-ci-badge]][travis-ci] [![GoDoc][godoc-badge]][godoc]
  2. ======
  3. GoMock is a mocking framework for the [Go programming language][golang]. It
  4. integrates well with Go's built-in `testing` package, but can be used in other
  5. contexts too.
  6. Installation
  7. ------------
  8. Once you have [installed Go][golang-install], run these commands
  9. to install the `gomock` package and the `mockgen` tool:
  10. go get github.com/golang/mock/gomock
  11. go install github.com/golang/mock/mockgen
  12. Documentation
  13. -------------
  14. After installing, you can use `go doc` to get documentation:
  15. go doc github.com/golang/mock/gomock
  16. Alternatively, there is an online reference for the package hosted on GoPkgDoc
  17. [here][gomock-ref].
  18. Running mockgen
  19. ---------------
  20. `mockgen` has two modes of operation: source and reflect.
  21. Source mode generates mock interfaces from a source file.
  22. It is enabled by using the -source flag. Other flags that
  23. may be useful in this mode are -imports and -aux_files.
  24. Example:
  25. mockgen -source=foo.go [other options]
  26. Reflect mode generates mock interfaces by building a program
  27. that uses reflection to understand interfaces. It is enabled
  28. by passing two non-flag arguments: an import path, and a
  29. comma-separated list of symbols.
  30. Example:
  31. mockgen database/sql/driver Conn,Driver
  32. The `mockgen` command is used to generate source code for a mock
  33. class given a Go source file containing interfaces to be mocked.
  34. It supports the following flags:
  35. * `-source`: A file containing interfaces to be mocked.
  36. * `-destination`: A file to which to write the resulting source code. If you
  37. don't set this, the code is printed to standard output.
  38. * `-package`: The package to use for the resulting mock class
  39. source code. If you don't set this, the package name is `mock_` concatenated
  40. with the package of the input file.
  41. * `-imports`: A list of explicit imports that should be used in the resulting
  42. source code, specified as a comma-separated list of elements of the form
  43. `foo=bar/baz`, where `bar/baz` is the package being imported and `foo` is
  44. the identifier to use for the package in the generated source code.
  45. * `-aux_files`: A list of additional files that should be consulted to
  46. resolve e.g. embedded interfaces defined in a different file. This is
  47. specified as a comma-separated list of elements of the form
  48. `foo=bar/baz.go`, where `bar/baz.go` is the source file and `foo` is the
  49. package name of that file used by the -source file.
  50. * `-build_flags`: (reflect mode only) Flags passed verbatim to `go build`.
  51. * `-mock_names`: A list of custom names for generated mocks. This is specified
  52. as a comma-separated list of elements of the form
  53. `Repository=MockSensorRepository,Endpoint=MockSensorEndpoint`, where
  54. `Repository` is the interface name and `MockSensorRepository` is the desired
  55. mock name (mock factory method and mock recorder will be named after the mock).
  56. If one of the interfaces has no custom name specified, then default naming
  57. convention will be used.
  58. * `-copyright_file`: Copyright file used to add copyright header to the resulting source code.
  59. For an example of the use of `mockgen`, see the `sample/` directory. In simple
  60. cases, you will need only the `-source` flag.
  61. Building Mocks
  62. --------------
  63. ```go
  64. type Foo interface {
  65. Bar(x int) int
  66. }
  67. func SUT(f Foo) {
  68. // ...
  69. }
  70. ```
  71. ```go
  72. func TestFoo(t *testing.T) {
  73. ctrl := gomock.NewController(t)
  74. // Assert that Bar() is invoked.
  75. defer ctrl.Finish()
  76. m := NewMockFoo(ctrl)
  77. // Asserts that the first and only call to Bar() is passed 99.
  78. // Anything else will fail.
  79. m.
  80. EXPECT().
  81. Bar(gomock.Eq(99)).
  82. Return(101)
  83. SUT(m)
  84. }
  85. ```
  86. Building Stubs
  87. --------------
  88. ```go
  89. type Foo interface {
  90. Bar(x int) int
  91. }
  92. Func SUT(f Foo) {
  93. // ...
  94. }
  95. ```
  96. ```go
  97. func TestFoo(t *testing.T) {
  98. ctrl := gomock.NewController(t)
  99. defer ctrl.Finish()
  100. m := NewMockFoo(ctrl)
  101. // Does not make any assertions. Returns 101 when Bar is invoked with 99.
  102. m.
  103. EXPECT().
  104. Bar(gomock.Eq(99)).
  105. Return(101).
  106. AnyTimes()
  107. // Does not make any assertions. Returns 103 when Bar is invoked with 101.
  108. m.
  109. EXPECT().
  110. Bar(gomock.Eq(101)).
  111. Return(103).
  112. AnyTimes()
  113. SUT(m)
  114. }
  115. ```
  116. [golang]: http://golang.org/
  117. [golang-install]: http://golang.org/doc/install.html#releases
  118. [gomock-ref]: http://godoc.org/github.com/golang/mock/gomock
  119. [travis-ci-badge]: https://travis-ci.org/golang/mock.svg?branch=master
  120. [travis-ci]: https://travis-ci.org/golang/mock
  121. [godoc-badge]: https://godoc.org/github.com/golang/mock/gomock?status.svg
  122. [godoc]: https://godoc.org/github.com/golang/mock/gomock