Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
/ moqit Public archive

Interface mocking tool for go generate http://bit.ly/meetmoq

License

Notifications You must be signed in to change notification settings

sudo-suhas/moqit

Repository files navigation

Archived

This repo has been archived as it is possible now to specify interface alias using moq(see matryer/moq#110):

$ moq -out ../mock/auth_mock.go -pkg mock . Client:AuthClient

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.

Forked

This fork allows to specify the name for the generated mock but removes the feature of generating mocks for multiple interfaces in a single command. To avoid confusion and to allow usage of both tools, the fork was renamed to moqit.

Compare against upstream master...sudo-suhas:master

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 Moqit, just run go get:

$ go get go get github.com/sudo-suhas/moqit

Usage

moqit [flags] destination interface
  -out string
    	output file (default stdout)
  -pkg string
    	package name (default will infer)
  -mock string
    	mock name (default will infer)

In a command line:

$ moqit -out mocks_test.go . MyInterface

In code (for go generate):

package my

//go:generate moqit -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.

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

This this example, Moqit 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.

About

Interface mocking tool for go generate http://bit.ly/meetmoq

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages