PegoMock is a mocking framework for the Go programming language. It integrates well with Go's built-in testing
package, but can be used in other contexts too. It is based on golang/mock, but uses a DSL closely related to Mockito.
Just go get
it:
go get github.com/petergtz/pegomock
go get github.com/petergtz/pegomock/pegomock # yes, that repetition of pegomock is necessary
This will download the package and install an executable pegomock
in your $GOPATH/bin
.
Use it like this:
import (
"github.com/petergtz/pegomock"
"testing"
)
func TestUsingMocks(t *testing.T) {
pegomock.RegisterMockTestingT(t)
// use Pegomock here
}
There are two caveats:
- You must register the
t *testing.T
passed to your test with Pegomock before you make any verifications associated with that test. So everyTest...
function in your suite should have theRegisterTestingT(t)
line. - Pegomock uses a global (singleton) fail handler. This has the benefit that you don’t need to pass the fail handler down to each test, but does mean that you cannot run your XUnit style tests in parallel with Pegomock.
When a Pegomock verification fails, it calls a FailHandler
. This is a function that you must provide using pegomock.RegisterMockFailHandler()
.
If you’re using Ginkgo, all you need to do is:
pegomock.RegisterMockFailHandler(ginkgo.Fail)
before you start your test suite.
Let's assume you have:
type Display interface {
Show(text string)
}
The simplest way is to call pegomock
from within your go package specifying the interface by its name:
cd path/to/package
pegomock generate Display
This will generate a mock_display_test.go
file which you can now use in your tests:
// creating mock
display := NewMockDisplay()
// using the mock
display.Show("Hello World!")
// verifying
display.VerifyWasCalledOnce().Show("Hello World!")
I've looked at some of the other frameworks, but found none of them satisfying:
-
GoMock seemed overly complicated when setting up mocks and verifying them. The command line interface is also not quite intuitive. That said, Pegomock is based on the GoMock, reusing mostly the mockgen code.
-
Counterfeiter uses a DSL that I didn't find expressive enough. It often seems to need more lines of code too. In one of its samples, it uses e.g.:
fake.DoThings("stuff", 5) Expect(fake.DoThingsCallCount()).To(Equal(1)) str, num := fake.DoThingsArgsForCall(0) Expect(str).To(Equal("stuff")) Expect(num).To(Equal(uint64(5)))
In Pegomock, this can be written as simple as:
fake.DoThings("stuff", 5) fake.VerifyWasCalledOnce().DoThings("stuff", 5)
-
Hel uses a new and interesting approach to setting up and verifying mocks. However, I wonder how flexible it actually is. E.g. how about providing a callback function when stubbing? Can this be modeled with its current approach using channels?
In addition, Pegomock provides a "watch" command similar to Ginkgo, which constantly watches over changes in an interface and updates its mocks. It gives the framework a much more dynamic feel, similar to mocking frameworks in Ruby or Java.
Interface:
type Display interface {
Show(text string)
}
Test:
// creating mock:
display := NewMockDisplay()
// using the mock:
display.Show("Hello World!")
// verifying:
display.VerifyWasCalledOnce().Show("Hello World!")
Interface:
type PhoneBook interface {
GetPhoneNumber(name string) string
}
Test:
// creating the mock
phoneBook := NewMockPhoneBook()
// stubbing:
When(phoneBook.GetPhoneNumber("Tom")).ThenReturn("345-123-789")
When(phoneBook.GetPhoneNumber("Invalid")).ThenPanic("Invalid Name")
// prints "345-123-789":
fmt.Println(phoneBook.GetPhoneNumber("Tom"))
// panics:
fmt.Println(phoneBook.GetPhoneNumber("Invalid"))
// prints "", because GetPhoneNumber("Dan") was not stubbed
fmt.Println(phoneBook.GetPhoneNumber("Dan"))
// Although it is possible to verify a stubbed invocation, usually it's redundant
// If your code cares what GetPhoneNumber("Tom") returns, then something else breaks (often even before a verification gets executed).
// If your code doesn't care what GetPhoneNumber("Tom") returns, then it should not be stubbed.
// Not convinced? See http://monkeyisland.pl/2008/04/26/asking-and-telling.
phoneBook.VerifyWasCalledOnce().GetPhoneNumber("Tom")
- By default, for all methods that return a value, a mock will return zero values.
- Once stubbed, the method will always return a stubbed value, regardless of how many times it is called.
Stubbing functions that have no return value requires a slightly different approach, because such functions cannot be passed directly to another function. However, we can wrap them in an anonymous function:
// creating mock:
display := NewMockDisplay()
// stubbing
When(func() { display.Show("Hello World!") }).ThenPanic("Panicking")
// panics:
display.Show("Hello World!")
Pegomock provides matchers for stubbing and verification.
Verification:
display := NewMockDisplay()
// Calling mock
display.Show("Hello again!")
// Verification:
display.VerifyWasCalledOnce().Show(AnyString())
Stubbing:
phoneBook := NewMockPhoneBook()
// Stubbing:
When(phoneBook.GetPhoneNumber(AnyString())).ThenReturn("123-456-789")
// Prints "123-456-789":
fmt.Println(phoneBook.GetPhoneNumber("Dan"))
// Also prints "123-456-789":
fmt.Println(phoneBook.GetPhoneNumber("Tom"))
Important: When you use argument matchers, you must always use them for all arguments:
// Incorrect, panics:
When(contactList.getContactByFullName("Dan", AnyString())).thenReturn(Contact{...})
// Correct:
When(contactList.getContactByFullName(EqString("Dan"), AnyString())).thenReturn(Contact{...})
Important: Eq...
and Any...
matchers for types used in mock methods, can now be auto-generated while generating the mock. So writing your own argument matchers is not necessary for most use cases. See section The Pegomock CLI for more information.
You can also write your own matchers for non-basic types. E.g. if you have a struct MyType
, you can write an Equals and Any matcher like this:
func EqMyType(value MyType) MyType {
RegisterMatcher(&EqMatcher{Value: value})
return MyType{}
}
func AnyMyType() MyType {
RegisterMatcher(NewAnyMatcher(reflect.TypeOf(MyType{})))
return MyType{}
}
display := NewMockDisplay()
// Calling mock
display.Show("Hello")
display.Show("Hello, again")
display.Show("And again")
// Verification:
display.VerifyWasCalled(Times(3)).Show(AnyString())
// or:
display.VerifyWasCalled(AtLeast(3)).Show(AnyString())
// or:
display.VerifyWasCalled(Never()).Show("This one was never called")
display1 := NewMockDisplay()
display2 := NewMockDisplay()
// Calling mocks
display1.Show("One")
display1.Show("Two")
display2.Show("Another two")
display1.Show("Three")
// Verification:
inOrderContext := new(InOrderContext)
display1.VerifyWasCalledInOrder(Once(), inOrderContext).Show("One")
display2.VerifyWasCalledInOrder(Once(), inOrderContext).Show("Another two")
display1.VerifyWasCalledInOrder(Once(), inOrderContext).Show("Three")
Note that it's not necessary to verify the call for display.Show("Two")
if that one is not of any interested. An InOrderContext
only verifies that the verifications that are done, are in order.
phoneBook := NewMockPhoneBook()
// Stubbing:
When(phoneBook.GetPhoneNumber(AnyString())).Then(func(params []Param) ReturnValues {
return []ReturnValue{fmt.Sprintf("1-800-CALL-%v", strings.ToUpper(params[0]))}
},
// Prints "1-800-CALL-DAN":
fmt.Println(phoneBook.GetPhoneNumber("Dan"))
// Prints "1-800-CALL-TOM":
fmt.Println(phoneBook.GetPhoneNumber("Tom"))
In some cases it can be useful to capture the arguments from mock invocations and assert on them separately. This method is only recommended if the techniques using matchers are not sufficient.
display := NewMockDisplay()
// Calling mock
display.Show("Hello")
display.Show("Hello, again")
display.Show("And again")
// Verification and getting captured arguments
text := display.VerifyWasCalled(AtLeast(1)).Show(AnyString()).GetCapturedArguments()
// Captured arguments are from last invocation
Expect(text).To(Equal("And again"))
You can also get all captured arguments:
// Verification and getting all captured arguments
texts := display.VerifyWasCalled(AtLeast(1)).Show(AnyString()).GetAllCapturedArguments()
// Captured arguments are a slice
Expect(texts).To(ConsistOf("Hello", "Hello, again", "And again"))
Install it via:
go install github.com/petergtz/pegomock/pegomock
Pegomock can generate mocks in two different ways:
-
by parsing source code Go files
pegomock generate [<flags>] <gofile>
-
by building a Go package and using reflection
pegomock generate [<flags>] [<packagepath>] <interfacename>
Flags can be any of the following:
-
--output,-o
: Output file; defaults to mock__test.go. -
--package
: Package of the generated code; defaults to the package from which pegomock was executed suffixed with _test -
--generate-matchers,-m
: This will auto-generate argument matchers and place them in amatchers
directory alongside the mock source code itself.
For more flags, run:
pegomock --help
There are a number of shortcomings in the current reflection-based implementation.
To overcome these, there is now an option to use a new, experimental implementation that is based on golang.org/x/tools/go/loader.
To use it when generating your mocks, invoke pegomock
like this:
pegomock generate --use-experimental-model-gen [<flags>] [<packagepath>] <interfacename>
What are the benefits?
- The current default uses the reflect package to introspect the interface for which a mock should be generated. But reflection cannot determine method parameter names, only types. This forces the generator to generate them based on a pattern. In a code editor with code assistence, those pattern-based names (such as
_param0
,_param1
) are non-descriptive and provide less help while writing code. The new implementation properly parses the source (including all dependent packages) and subsequently uses the same names as used in the interface definition. - With the current default you cannot generate an interface that lives in the
main
package. It's due to the way this implementation works: it imports the interface's package into temporarily generated code that gets compiled on the fly. This compilation fails, because there are now twomain
functions. - The new implementation is simpler and will probably become the default in the future, because it will be easier to maintain.
What are the drawbacks?
- There is only one drawback: maturity. The new implementation is not complete yet, and also might have some bugs that still need to be fixed.
Users of Pegomock are encouraged to use this new option and report any problems by opening an issue. Help to stabilize it is greatly appreciated.
pegomock
can be used with go generate
. Simply add the directive to your source file.
Here's an example for a Display interface used by a calculator program:
// package/path/to/display/display.go
package display
type Display interface {
Show(text string)
}
// package/path/to/calculator/calculator_test.go
package calculator_test
//go:generate pegomock generate package/path/to/display Display
// Use generated mock
mockDisplay := NewMockDisplay()
...
Generating it:
cd package/path/to/calculator
go generate
Note: While you could add the directive adjacent to the interface definition, the author's opinion is that this violates clean dependency management and would pollute the package of the interface. It's better to generate the mock in the same package, where it is used (if this coincides with the interface package, that's fine). That way, not only stays the interface's package clean, the tests also don't need to prefix the mock with a package, or use a dot-import.
The watch
command lets Pegomock generate mocks continuously on every change to an interface:
pegomock watch
For this, Pegomock expects an interfaces_to_mock
file in the package directory where the mocks should be generated. In fact, pegomock watch
will create it for you if it doesn't exist yet. The contents of the file are similar to the ones of the generate
command:
# Any line starting with a # is treated as comment.
# interface name without package specifies an Interface in the current package:
PhoneBook
# generates a mock for SomeInterfacetaken from mypackage:
path/to/my/mypackage SomeInterface
# you can also specify a Go file:
display.go
# and use most of the flags from the "generate" command
--output my_special_output.go MyInterface
Flags can be:
--recursive,-r
: Recursively watch sub-directories as well.