Go to file
Mat Ryer c80e9a745b
Merge pull request #35 from dpetersen/appease-golint
Ensure generated content is friendlier to golint
2017-12-06 15:12:51 +00:00
example Go fmt 2017-07-11 21:42:55 +01:00
generate improved report card 2016-11-06 12:37:57 +00:00
pkg Merge branch 'master' into appease-golint 2017-12-06 15:07:50 +00:00
.gitignore minor tweaks 2016-10-14 10:57:57 +01:00
.travis.yml removed go 1.6 2017-07-07 16:43:42 +01:00
LICENSE added credit 2016-09-01 10:30:27 +01:00
main.go added vendor example 2017-07-11 20:02:46 +01:00
moq-logo-small.png new logo 2016-09-01 10:13:13 +01:00
moq-logo.png new logo 2016-09-01 10:13:13 +01:00
preview.png New preview image 2017-07-11 21:31:14 +01:00
README.md Update the example in the README 2017-09-11 09:54:35 +10:00

moq logo Build Status Go Report Card

Interface mocking tool for go generate.

By Mat Ryer and David Hernandez, with ideas lovingly stolen from Ernesto Jimenez.

What is Moq?

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.

Preview

above: Moq generates the code on the right.

You can read more in the Meet Moq blog post.

Installing

To start using Moq, just run go get:

$ go get github.com/matryer/moq

Usage

moq [flags] destination interface [interface2 [interface3 [...]]]
  -out string
    	output file (default stdout)
  -pkg string
    	package name (default will infer)

In a command line:

$ moq -out mocks_test.go . MyInterface

In code (for go generate):

package my

//go:generate moq -out myinterface_moq_test.go . MyInterface

type MyInterface interface {
	Method1() error
	Method2(i int)
}

Then run go generate for your package.

How to use it

Mocking interfaces is a nice way to write unit tests where you can easily control the behaviour of the mocked object.

Moq creates a struct that has a function field for each method, which you can declare in your test code.

This this example, Moq generated the EmailSenderMock type:

func TestCompleteSignup(t *testing.T) {

	var sentTo string 

	mockedEmailSender = &EmailSenderMock{
		SendFunc: func(to, subject, body string) error {
			sentTo = to
			return nil
		},
	}

	CompleteSignUp("me@email.com", mockedEmailSender)

	callsToSend := len(mockedEmailSender.SendCalls())
	if callsToSend != 1 {
		t.Errorf("Send was called %d times", callsToSend)
	}
	if sentTo != "me@email.com" {
		t.Errorf("unexpected recipient: %s", sentTo)
	}

}

func CompleteSignUp(to string, sender EmailSender) {
	// TODO: this
}

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
  • Only mock the fields you need
  • It will panic if a nil function gets called
  • Name arguments in the interface for a better experience
  • Use closured variables inside your test function to capture details about the calls to the methods
  • Use .MethodCalls() to track the calls
  • Use go:generate to invoke the moq command

License

The Moq project (and all code) is licensed under the MIT License.

The Moq logo was created by Chris Ryer and is licensed under the Creative Commons Attribution 3.0 License.