pegomock

package module
v0.0.0-...-22e6f2b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2018 License: Apache-2.0 Imports: 11 Imported by: 0

README

Build Status

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.

Getting Pegomock

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.

Getting Started

Using Pegomock with Golang’s XUnit-style Tests

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 every Test... function in your suite should have the RegisterTestingT(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.

Using Pegomock with Ginkgo

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.

Note: Ginkgo introduced a new keyword in its DSL: When. This causes name collisions when dot-importing both Ginkgo and Pegomock. To avoid this, you can follow these Ginkgo import instructions.

Generating Your First Mock and Using It

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!")

Why yet Another Mocking Framework for Go?

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.

Using Mocks In Your Tests

Verifying Behavior

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!")

Stubbing

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

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!")

Argument Matchers

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{...})
Writing Your Own Argument Matchers

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{}
}

Verifying the Number of Invocations

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")

Verifying in Order

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.

Stubbing with Callbacks

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"))

Verifying with Argument Capture

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"))

The Pegomock CLI

Installation

Install it via:

go install github.com/petergtz/pegomock/pegomock

Generating Mocks

Pegomock can generate mocks in two different ways:

  1. by parsing source code Go files

    pegomock generate [<flags>] <gofile>
    
  2. 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 a matchers directory alongside the mock source code itself.

For more flags, run:

pegomock --help

Generating Mocks with --use-experimental-model-gen

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 two main 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.

Generating mocks with go generate

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.

Continuously Generating Mocks

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyBool

func AnyBool() bool

func AnyBoolSlice

func AnyBoolSlice() []bool

func AnyComplex128

func AnyComplex128() complex128

func AnyComplex128Slice

func AnyComplex128Slice() []complex128

func AnyComplex64

func AnyComplex64() complex64

func AnyComplex64Slice

func AnyComplex64Slice() []complex64

func AnyFloat32

func AnyFloat32() float32

func AnyFloat32Slice

func AnyFloat32Slice() []float32

func AnyFloat64

func AnyFloat64() float64

func AnyFloat64Slice

func AnyFloat64Slice() []float64

func AnyInt

func AnyInt() int

func AnyInt16

func AnyInt16() int16

func AnyInt16Slice

func AnyInt16Slice() []int16

func AnyInt32

func AnyInt32() int32

func AnyInt32Slice

func AnyInt32Slice() []int32

func AnyInt64

func AnyInt64() int64

func AnyInt64Slice

func AnyInt64Slice() []int64

func AnyInt8

func AnyInt8() int8

func AnyInt8Slice

func AnyInt8Slice() []int8

func AnyIntSlice

func AnyIntSlice() []int

func AnyString

func AnyString() string

func AnyStringSlice

func AnyStringSlice() []string

func AnyUint

func AnyUint() uint

func AnyUint16

func AnyUint16() uint16

func AnyUint16Slice

func AnyUint16Slice() []uint16

func AnyUint32

func AnyUint32() uint32

func AnyUint32Slice

func AnyUint32Slice() []uint32

func AnyUint64

func AnyUint64() uint64

func AnyUint64Slice

func AnyUint64Slice() []uint64

func AnyUint8

func AnyUint8() uint8

func AnyUint8Slice

func AnyUint8Slice() []uint8

func AnyUintSlice

func AnyUintSlice() []uint

func AnyUintptr

func AnyUintptr() uintptr

func AnyUintptrSlice

func AnyUintptrSlice() []uintptr

func DumpInvocationsFor

func DumpInvocationsFor(mock Mock)

func EqBool

func EqBool(value bool) bool

func EqComplex128

func EqComplex128(value complex128) complex128

func EqComplex64

func EqComplex64(value complex64) complex64

func EqFloat32

func EqFloat32(value float32) float32

func EqFloat64

func EqFloat64(value float64) float64

func EqInt

func EqInt(value int) int

func EqInt16

func EqInt16(value int16) int16

func EqInt32

func EqInt32(value int32) int32

func EqInt64

func EqInt64(value int64) int64

func EqInt8

func EqInt8(value int8) int8

func EqString

func EqString(value string) string

func EqUint

func EqUint(value uint) uint

func EqUint16

func EqUint16(value uint16) uint16

func EqUint32

func EqUint32(value uint32) uint32

func EqUint64

func EqUint64(value uint64) uint64

func EqUint8

func EqUint8(value uint8) uint8

func EqUintptr

func EqUintptr(value uintptr) uintptr

func RegisterMatcher

func RegisterMatcher(matcher Matcher)

func RegisterMockFailHandler

func RegisterMockFailHandler(handler FailHandler)

func RegisterMockTestingT

func RegisterMockTestingT(t *testing.T)

func SDumpInvocationsFor

func SDumpInvocationsFor(mock Mock) string

func When

func When(invocation ...interface{}) *ongoingStubbing

Types

type AnyMatcher

type AnyMatcher struct {
	Type reflect.Type

	sync.Mutex
	// contains filtered or unexported fields
}

func NewAnyMatcher

func NewAnyMatcher(typ reflect.Type) *AnyMatcher

func (*AnyMatcher) FailureMessage

func (matcher *AnyMatcher) FailureMessage() string

func (*AnyMatcher) Matches

func (matcher *AnyMatcher) Matches(param Param) bool

func (*AnyMatcher) String

func (matcher *AnyMatcher) String() string

type AtLeastIntMatcher

type AtLeastIntMatcher struct {
	Value int
	// contains filtered or unexported fields
}

func AtLeast

func AtLeast(numDesiredInvocations int) *AtLeastIntMatcher

func (*AtLeastIntMatcher) FailureMessage

func (matcher *AtLeastIntMatcher) FailureMessage() string

func (*AtLeastIntMatcher) Matches

func (matcher *AtLeastIntMatcher) Matches(param Param) bool

func (*AtLeastIntMatcher) String

func (matcher *AtLeastIntMatcher) String() string

type AtMostIntMatcher

type AtMostIntMatcher struct {
	Value int
	// contains filtered or unexported fields
}

func AtMost

func AtMost(numDesiredInvocations int) *AtMostIntMatcher

func (*AtMostIntMatcher) FailureMessage

func (matcher *AtMostIntMatcher) FailureMessage() string

func (*AtMostIntMatcher) Matches

func (matcher *AtMostIntMatcher) Matches(param Param) bool

func (*AtMostIntMatcher) String

func (matcher *AtMostIntMatcher) String() string

type Counter

type Counter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

type EqMatcher

type EqMatcher struct {
	Value Param

	sync.Mutex
	// contains filtered or unexported fields
}

func Never

func Never() *EqMatcher

func Once

func Once() *EqMatcher

func Times

func Times(numDesiredInvocations int) *EqMatcher

func Twice

func Twice() *EqMatcher

func (*EqMatcher) FailureMessage

func (matcher *EqMatcher) FailureMessage() string

func (*EqMatcher) Matches

func (matcher *EqMatcher) Matches(param Param) bool

func (*EqMatcher) String

func (matcher *EqMatcher) String() string

type FailHandler

type FailHandler func(message string, callerSkip ...int)
var GlobalFailHandler FailHandler

func BuildTestingTGomegaFailHandler

func BuildTestingTGomegaFailHandler(t testingT) FailHandler

type GenericMock

type GenericMock struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func GetGenericMockFrom

func GetGenericMockFrom(mock Mock) *GenericMock

func (*GenericMock) GetInvocationParams

func (genericMock *GenericMock) GetInvocationParams(methodInvocations []MethodInvocation) [][]Param

TODO this doesn't need to be a method, can be a free function

func (*GenericMock) Invoke

func (genericMock *GenericMock) Invoke(methodName string, params []Param, returnTypes []reflect.Type) ReturnValues

func (*GenericMock) Verify

func (genericMock *GenericMock) Verify(
	inOrderContext *InOrderContext,
	invocationCountMatcher Matcher,
	methodName string,
	params []Param) []MethodInvocation

type InOrderContext

type InOrderContext struct {
	// contains filtered or unexported fields
}

type Matcher

type Matcher interface {
	Matches(param Param) bool
	FailureMessage() string
	fmt.Stringer
}

Matcher ... it is guaranteed that FailureMessage will always be called after Matches so an implementation can save state

type Matchers

type Matchers []Matcher

func (Matchers) Matches

func (matchers Matchers) Matches(params []Param) bool

type MethodInvocation

type MethodInvocation struct {
	// contains filtered or unexported fields
}

type Mock

type Mock interface{}

type Param

type Param interface{}

type ReturnValue

type ReturnValue interface{}

type ReturnValues

type ReturnValues []ReturnValue

type Stubbing

type Stubbing struct {
	// contains filtered or unexported fields
}

func (*Stubbing) Invoke

func (stubbing *Stubbing) Invoke(params []Param) ReturnValues

type Stubbings

type Stubbings []*Stubbing

Directories

Path Synopsis
internal
MockGen generates mock implementations of Go interfaces.
MockGen generates mock implementations of Go interfaces.
Package model contains the data model necessary for generating mock implementations.
Package model contains the data model necessary for generating mock implementations.
modelgen

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL