Giter VIP home page Giter VIP logo

mockery's Introduction

mockery

mockery provides the ability to easily generate mocks for golang interfaces. It removes the boilerplate coding required to use mocks.

Linux Build Status Windows Build Status GoDoc Go Report Card

Installation

go get github.com/vektra/mockery/.../, then $GOPATH/bin/mockery

Examples

Simplest case

Given this is in string.go

package test

type Stringer interface {
	String() string
}

Run: mockery -name=Stringer and the following will be output to mocks/Stringer.go:

package mocks

import "github.com/stretchr/testify/mock"

type Stringer struct {
	mock.Mock
}

func (m *Stringer) String() string {
	ret := m.Called()

	var r0 string
	if rf, ok := ret.Get(0).(func() string); ok {
		r0 = rf()
	} else {
		r0 = ret.Get(0).(string)
	}

	return r0
}

Next level case

See github.com/jaytaylor/mockery-example for the fully runnable version of the outline below.

package main

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/jaytaylor/mockery-example/mocks"
	"github.com/stretchr/testify/mock"
)

func main() {
	mockS3 := &mocks.S3API{}

	mockResultFn := func(input *s3.ListObjectsInput) *s3.ListObjectsOutput {
		output := &s3.ListObjectsOutput{}
		output.SetCommonPrefixes([]*s3.CommonPrefix{
			&s3.CommonPrefix{
				Prefix: aws.String("2017-01-01"),
			},
		})
		return output
	}

	// NB: .Return(...) must return the same signature as the method being mocked.
	//     In this case it's (*s3.ListObjectsOutput, error).
	mockS3.On("ListObjects", mock.MatchedBy(func(input *s3.ListObjectsInput) bool {
		return input.Delimiter != nil && *input.Delimiter == "/" && input.Prefix == nil
	})).Return(mockResultFn, nil)

	listingInput := &s3.ListObjectsInput{
		Bucket:    aws.String("foo"),
		Delimiter: aws.String("/"),
	}
	listingOutput, err := mockS3.ListObjects(listingInput)
	if err != nil {
		panic(err)
	}

	for _, x := range listingOutput.CommonPrefixes {
		fmt.Printf("common prefix: %+v\n", *x)
	}
}

Imports

mockery pulls in all the same imports used in the file that contains the interface so that package types will work correctly. It then runs the output through the imports package to remove any unnecessary imports (as they'd result in compile errors).

Types

mockery should handle all types. If you find it does not, please report the issue.

Return Value Provider Functions

If your tests need access to the arguments to calculate the return values, set the return value to a function that takes the method's arguments as its own arguments and returns the return value. For example, given this interface:

package test

type Proxy interface {
  passthrough(s string) string
}

The argument can be passed through as the return value:

import . "github.com/stretchr/testify/mock"

Mock.On("passthrough", AnythingOfType("string")).Return(func(s string) string {
    return s
})

Requirements

Return must be passed the same argument count and types as expected by the interface. If the return argument signature of passthrough in the above example was instead (string, error) in the interface, Return would also need a second argument to define the error value.

If any return argument is missing, github.com/stretchr/testify/mock.Arguments.Get will emit a panic.

For example, panic: assert: arguments: Cannot call Get(0) because there are 0 argument(s). [recovered] indicates that Return was not provided any arguments but (at least one) was expected based on the interface. Get(1) would indicate that the Return call is missing a second argument, and so on.

Notes

This approach should be used judiciously, as return values should generally not depend on arguments in mocks; however, this approach can be helpful for situations like passthroughs or other test-only calculations.

Name

The -name option takes either the name or matching regular expression of interface to generate mock(s) for.

All

It's common for a big package to have a lot of interfaces, so mockery provides -all. This option will tell mockery to scan all files under the directory named by -dir ("." by default) and generates mocks for any interfaces it finds. This option implies -recursive=true.

-all was designed to be able to be used automatically in the background if required.

Recursive

Use the -recursive option to search subdirectories for the interface(s). This option is only compatible with -name. The -all option implies -recursive=true.

Output

mockery always generates files with the package mocks to keep things clean and simple. You can control which mocks directory is used by using -output, which defaults to ./mocks.

Caseing

mockery generates files using the caseing of the original interface name. This can be modified by specifying -case=underscore to format the generated file name using underscore casing.

Debug

Use mockery -print to have the resulting code printed out instead of written to disk.

Mocking interfaces in main

When your interfaces are in the main package you should supply the -inpkg flag. This will generate mocks in the same package as the target code avoiding import issues.

mockery's People

Contributors

abourget avatar ajoensson avatar ajrharris avatar anupcshan avatar cam72cam avatar codeactual avatar colonelpanic8 avatar cstrahan avatar dakiva avatar dwlnetnl avatar ernesto-jimenez avatar evanphx avatar fumoboy007 avatar gaffo avatar houjunchen avatar itsarbit avatar jaytaylor avatar joshk0 avatar jriquelme avatar kasey avatar kevinburke avatar mveytsman avatar ryanbrainard avatar tamccall avatar tehleach avatar yamada-wacul avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.