Coder Social home page Coder Social logo

gomocker's Introduction

Go Mocker

Gomocker generates Golang code to easily mock a function or interface

Installing

go get github.com/jauhararifin/gomocker/gomocker

Example

  1. Suppose you have an interface that look like this
type Math interface {
	Add(ctx context.Context, a, b int) (sum int, err error)
	Subtract(ctx context.Context, a, b int) (result int, err error)
}
  1. Put a go generate script to generate mocker code for that interface
//go:generate gomocker gen --force --package github.com/jauhararifin/gomocker/examples github.com/jauhararifin/gomocker/examples:Math

type Math interface {
	Add(ctx context.Context, a, b int) (sum int, err error)
	Subtract(ctx context.Context, a, b int) (result int, err error)
}
  1. Run go generate ./...

  2. A file called math_mock_gen.go containing all the code to mock Math interface will be generated.

  3. Use the mocker

func TestMathMocked_Add(t *testing.T) {
	// create the mocked `Math` and it's mocker.
	math, mocker := NewMockedMath()

	// mock the output value of `Add` method once.
	mocker.Add.MockOutputsOnce(10, nil)

	// call the Add method. 
	sum, err := math.Add(context.Background(), 17, 19)

	// it should return 10 because we mock it previously.
	assert.Nil(t, err, "wrong error")
	assert.Equal(t, 10, sum, "wrong sum")
	
	// use `mocker.Add.TakeOnceInvocation` to get the "invocation struct". The "invocation struct" contains all the
	// input and output of an invocation.
	invoc := mocker.Add.TakeOneInvocation()
	assert.Equal(t, context.Background(), invoc.Inputs.Ctx, "wrong context")
	assert.Equal(t, 17, invoc.Inputs.A, "wrong first integer")
	assert.Equal(t, 19, invoc.Inputs.B, "wrong second integer")
	assert.Equal(t, 10, invoc.Outputs.Sum, "wrong first returns")
	assert.Equal(t, nil, invoc.Outputs.Err, "wrong second returns")
}

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.