2020-07-29 15:37:21 +05:30
![moq logo ](moq-logo-small.png ) [![build ](https://github.com/matryer/moq/workflows/build/badge.svg )](https://github.com/matryer/moq/actions?query=branch%3Amaster) [![Go Report Card ](https://goreportcard.com/badge/github.com/matryer/moq )](https://goreportcard.com/report/github.com/matryer/moq)
2016-08-30 13:00:43 +01:00
2016-08-29 13:25:35 +01:00
Interface mocking tool for go generate.
2016-09-22 09:29:05 +01:00
### What is Moq?
2016-09-21 16:03:39 +01:00
Moq is a tool that generates a struct from any interface. The struct can be used in test code as a mock of the interface.
2016-10-14 10:59:38 +01:00
![Preview ](preview.png )
above: Moq generates the code on the right.
2016-09-22 16:35:56 +09:00
2016-10-14 14:01:13 +01:00
You can read more in the [Meet Moq blog post ](http://bit.ly/meetmoq ).
2016-09-22 16:35:56 +09:00
### Installing
2021-06-25 16:31:29 +09:00
To start using latest released version of Moq, just run:
#### Go version < 1.16
2016-09-22 16:35:56 +09:00
```
$ go get github.com/matryer/moq
```
2021-06-25 16:31:29 +09:00
#### Go 1.16+
```
$ go install github.com/matryer/moq@latest
```
2016-08-30 13:00:43 +01:00
### Usage
2016-08-29 13:00:18 +01:00
2016-09-21 22:10:12 +01:00
```
2020-03-10 18:38:14 +05:30
moq [flags] source-dir interface [interface2 [interface3 [...]]]
2020-09-18 09:52:55 +01:00
-fmt string
go pretty-printer: gofmt, goimports or noop (default gofmt)
-out string
output file (default stdout)
-pkg string
package name (default will infer)
-stub
return zero values when no mock implementation is provided, do not panic
-skip-ensure
2021-06-25 16:31:29 +09:00
suppress mock implementation check, avoid import cycle if mocks
2020-09-18 09:53:31 +01:00
generated outside of the tested package
2020-09-17 03:49:08 -05:00
2019-11-05 13:13:49 +05:30
Specifying an alias for the mock is also supported with the format 'interface:alias'
2020-09-18 09:58:15 +01:00
Example: moq -pkg different . MyInterface:MyMock
2016-09-21 22:10:12 +01:00
```
2021-06-25 16:31:29 +09:00
**NOTE:** `source-dir` is the directory where the source code (definition) of the target interface is located.
2020-03-10 18:38:14 +05:30
It needs to be a path to a directory and not the import statement for a Go package.
2016-08-30 13:04:56 +01:00
In a command line:
2016-08-29 13:00:18 +01:00
```
2016-09-22 16:35:56 +09:00
$ moq -out mocks_test.go . MyInterface
2016-08-29 13:25:35 +01:00
```
2016-08-30 13:04:56 +01:00
In code (for go generate):
```go
package my
2016-09-21 22:07:00 +01:00
//go:generate moq -out myinterface_moq_test.go . MyInterface
2016-08-30 13:04:56 +01:00
type MyInterface interface {
Method1() error
Method2(i int)
}
```
Then run `go generate` for your package.
2016-08-30 13:00:43 +01:00
### How to use it
2016-08-29 13:30:24 +01:00
Mocking interfaces is a nice way to write unit tests where you can easily control the behaviour of the mocked object.
2016-08-29 13:31:32 +01:00
Moq creates a struct that has a function field for each method, which you can declare in your test code.
2019-11-03 08:54:11 +01:00
In this example, Moq generated the `EmailSenderMock` type:
2016-08-29 13:30:24 +01:00
```go
func TestCompleteSignup(t *testing.T) {
2019-11-05 13:13:49 +05:30
var sentTo string
2016-08-29 13:30:24 +01:00
2016-08-29 13:31:32 +01:00
mockedEmailSender = & EmailSenderMock{
2016-08-29 13:30:24 +01:00
SendFunc: func(to, subject, body string) error {
sentTo = to
return nil
},
}
CompleteSignUp("me@email .com", mockedEmailSender)
2017-09-11 09:54:35 +10:00
callsToSend := len(mockedEmailSender.SendCalls())
if callsToSend != 1 {
t.Errorf("Send was called %d times", callsToSend)
2016-08-29 13:30:24 +01:00
}
if sentTo != "me@email .com" {
t.Errorf("unexpected recipient: %s", sentTo)
}
}
2016-08-29 13:31:32 +01:00
func CompleteSignUp(to string, sender EmailSender) {
// TODO: this
}
2016-08-29 13:30:24 +01:00
```
2016-08-29 13:33:28 +01:00
The mocked structure implements the interface, where each method calls the associated function field.
## Tips
* Keep mocked logic inside the test that is using it
2017-07-07 14:49:48 +01:00
* Only mock the fields you need
* It will panic if a nil function gets called
2017-07-07 15:12:32 +01:00
* Name arguments in the interface for a better experience
2016-08-29 13:34:34 +01:00
* Use closured variables inside your test function to capture details about the calls to the methods
2017-07-11 21:30:08 +01:00
* Use `.MethodCalls()` to track the calls
2016-08-29 13:33:28 +01:00
* Use `go:generate` to invoke the `moq` command
2021-06-25 16:31:29 +09:00
* If Moq fails with a `go/format` error, it indicates the generated code was not valid.
You can run the same command with `-fmt noop` to print the generated source code without attempting to format it.
This can aid in debugging the root cause.
2016-09-01 10:30:27 +01:00
## License
2016-09-22 09:29:05 +01:00
The Moq project (and all code) is licensed under the [MIT License ](LICENSE ).
2016-09-01 10:30:27 +01:00
2021-02-01 19:22:42 +00:00
Moq was created by [Mat Ryer ](https://twitter.com/matryer ) and [David Hernandez ](https://github.com/dahernan ), with ideas lovingly stolen from [Ernesto Jimenez ](https://github.com/ernesto-jimenez ). Featuring a major refactor by @sudo -suhas, as well as lots of other contributors.
2016-09-22 09:29:05 +01:00
The Moq logo was created by [Chris Ryer ](http://chrisryer.co.uk ) and is licensed under the [Creative Commons Attribution 3.0 License ](https://creativecommons.org/licenses/by/3.0/ ).
2021-02-01 19:22:42 +00:00