Go to file
2016-08-29 13:30:24 +01:00
example improved output 2016-08-29 13:09:34 +01:00
package/moq removed generated by bit 2016-08-29 13:22:52 +01:00
.gitignore Initial commit 2016-08-29 11:00:17 +01:00
LICENSE Initial commit 2016-08-29 11:00:17 +01:00
main.go early commit for moq command 2016-08-29 13:00:18 +01:00
README.md added help 2016-08-29 13:30:24 +01:00

moq

Interface mocking tool for go generate.

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

Usage

moq InterfaceName -out mocks_test.go

Install

go install github.com/matryer/moq

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:

func TestCompleteSignup(t *testing.T) {

	called := false
	var sentTo string 

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

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

	if called == false {
		t.Error("Sender.Send expected")
	}
	if sentTo != "me@email.com" {
		t.Errorf("unexpected recipient: %s", sentTo)
	}

}

The mocked structure implements the interface, where each method calls the associated function field.