All Projects → matryer → Moq

matryer / Moq

Licence: mit
Interface mocking tool for go generate

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Moq

Weaver
Dependency Injection framework for Swift (iOS/macOS/Linux)
Stars: ✭ 546 (-41.98%)
Mutual labels:  codegen
Cmockery
A lightweight library to simplify and generalize the process of writing unit tests for C applications.
Stars: ✭ 697 (-25.93%)
Mutual labels:  mocking
Graphql Java Codegen Maven Plugin
Maven plugin for graphql-java-codegen
Stars: ✭ 17 (-98.19%)
Mutual labels:  codegen
Ts Mockito
Mocking library for TypeScript
Stars: ✭ 608 (-35.39%)
Mutual labels:  mocking
Wire
Compile-time Dependency Injection for Go
Stars: ✭ 7,091 (+653.56%)
Mutual labels:  codegen
Astring
🌳 Tiny and fast JavaScript code generator from an ESTree-compliant AST.
Stars: ✭ 757 (-19.55%)
Mutual labels:  codegen
Ohhttpstubs
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Stars: ✭ 4,831 (+413.39%)
Mutual labels:  mocking
Pose
Replace any .NET method (including static and non-virtual) with a delegate
Stars: ✭ 855 (-9.14%)
Mutual labels:  mocking
Gqlgen
go generate based graphql server library
Stars: ✭ 6,880 (+631.14%)
Mutual labels:  codegen
Graphql Code Generator
A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
Stars: ✭ 7,993 (+749.42%)
Mutual labels:  codegen
Msw
Seamless REST/GraphQL API mocking library for browser and Node.js.
Stars: ✭ 7,830 (+732.09%)
Mutual labels:  mocking
Mockhttp
Testing layer for Microsoft's HttpClient library. Create canned responses using a fluent API.
Stars: ✭ 623 (-33.79%)
Mutual labels:  mocking
Esview
此项目已暂停开发
Stars: ✭ 761 (-19.13%)
Mutual labels:  codegen
Mock Socket
Javascript mocking library for WebSockets and Socket.IO
Stars: ✭ 598 (-36.45%)
Mutual labels:  mocking
Pytest Responsemock
Simplified requests calls mocking for pytest
Stars: ✭ 24 (-97.45%)
Mutual labels:  mocking
Kakapo.js
🐦 Next generation mocking framework in Javascript
Stars: ✭ 535 (-43.15%)
Mutual labels:  mocking
Inkwell
It's a New Kind of Wrapper for Exposing LLVM (Safely)
Stars: ✭ 732 (-22.21%)
Mutual labels:  codegen
Faker.js
generate massive amounts of realistic fake data in Node.js and the browser
Stars: ✭ 34,329 (+3548.14%)
Mutual labels:  mocking
Clj Fakes
An isolation framework for Clojure/ClojureScript.
Stars: ✭ 26 (-97.24%)
Mutual labels:  mocking
Graphqlgen
⚙️ Generate type-safe resolvers based upon your GraphQL Schema
Stars: ✭ 796 (-15.41%)
Mutual labels:  codegen

moq logo build Go Report Card

Interface mocking tool for go generate.

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] source-dir interface [interface2 [interface3 [...]]]
	-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
		suppress mock implementation check, avoid import cycle if mocks 
		generated outside of the tested package

Specifying an alias for the mock is also supported with the format 'interface:alias'

Example: moq -pkg different . MyInterface:MyMock

NOTE: source-dir is the directory where the source code (definition) of the target interface is located. It needs to be a path to a directory and not the import statement for a Go package.

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.

In 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("[email protected]", mockedEmailSender)

	callsToSend := len(mockedEmailSender.SendCalls())
	if callsToSend != 1 {
		t.Errorf("Send was called %d times", callsToSend)
	}
	if sentTo != "[email protected]" {
		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
  • 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.

License

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

Moq was created by Mat Ryer and David Hernandez, with ideas lovingly stolen from Ernesto Jimenez. Featuring a major refactor by @sudo-suhas, as well as lots of other contributors.

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].