spies

package module
v0.0.0-...-112961b Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2020 License: MIT Imports: 6 Imported by: 6

README

Spies

Package spies provides a testing spy framework for Go. It is heavily influenced by (testify/mock.)[https://github.com/stretchr/testify#mock-package]

Roughly speaking, you implement an interface against a spy, then you can use the internal Spy object to record responses to return, and then assert calls against the spy.

Motivation

Why this package and not testify/mock? I personally find that Spies are more flexbile, and encourage you to actually test the things that need to be tested. For instance, often a test double needs to return a value in response to a query (e.g. wehave.HowManyBananas() returns 0). If the code under test changes and no longer needs to make that query, there's no reason that the tests should break.

Future Work

It'd be keen to have code generation for spies, since they wind up usually being very standard structures. However, I've often found it handy to extend from the basic patterns with little utility methods on the control struct.

It might also be nice to have a few extra methods related to selecting and inspecting calls, since that can get a little hairy.

Documentation

Overview

Package spies provides test spies for go test, in the vien of testify's mocks. Roughly speaking, define a spy like this:

type MySpy {
	*spies.Spy
}

func NewMySpy() *MySpy {
  return &MySpy{ NewSpy() }
}

func (my *MySpy) InterfaceMethod(with, some, args int) (ret string, err error) {
  res := my.Called(with, some, args)
  return res.String(0), res.Error(1)
}

Use your spy in tests like:

my.MatchMethod("InterfaceMethod", spies.AnyArgs, "called", nil)

... which will return "called" with a nil error whenever InterfaceMethod is called. Several calls to Match and MatchMethod can be made in a row - the first match wins. Then you can check calls by calling my.Calls to my.CallsTo

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Always

func Always(string, mock.Arguments) bool

Always is an always-true predicate

func AnyArgs

func AnyArgs(mock.Arguments) bool

AnyArgs is an always-true predicate for MethodMatch

func CallCount

func CallCount(n int) func(mock.Arguments) bool

CallCount constructs a predicate that allows a method to be called a certain number of times.

func Once

func Once() func(mock.Arguments) bool

Once is a convenience for CallCount(1)

Types

type Call

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

func (Call) PassedArgs

func (c Call) PassedArgs() mock.Arguments

func (Call) String

func (c Call) String() string

type PassedArger

type PassedArger interface {
	PassedArgs() mock.Arguments
}

A PassedArger implements PassedArgs

type Spy

type Spy struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

A Spy is a type for use in testing - it's intended to be embedded in spy implementations.

func NewSpy

func NewSpy() *Spy

NewSpy makes a Spy

func (*Spy) Any

func (s *Spy) Any(method string, result ...interface{})

Any records that any Call to method get result as a reply

func (*Spy) Called

func (s *Spy) Called(argList ...interface{}) results

Called is used by embedders of Spy to indicate that the method is called.

func (*Spy) Calls

func (s *Spy) Calls() []Call

Calls returns all the calls made to the spy

func (*Spy) CallsMatching

func (s *Spy) CallsMatching(f func(name string, args mock.Arguments) bool) []Call

CallsMatching returns a list of calls for with f() returns true.

func (*Spy) CallsTo

func (s *Spy) CallsTo(name string) []Call

CallsTo returns the calls to the named method

func (*Spy) Match

func (s *Spy) Match(pred func(string, mock.Arguments) bool, result ...interface{})

Match records an arbitrary predicate to match against a method Call

func (*Spy) MatchMethod

func (s *Spy) MatchMethod(method string, pred func(mock.Arguments) bool, result ...interface{})

MatchMethod records a predicate limited to a specific method name

func (*Spy) String

func (s *Spy) String() string

Jump to

Keyboard shortcuts

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