mock

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArgumentMatcher added in v1.3.0

type ArgumentMatcher interface {
	Match(arg any) bool
}

An argument matcher it's an helper value that should be used when asserting a mock was called with a specific set of values.

Users can define their own argument matchers when they want to define what exactly should be matched when comparing the mock call argument with the expected argument.

type MatchAny added in v1.3.0

type MatchAny struct{}

MatchAny it's an argument matcher that matches anything. Use it on CalledWith or CalledWithExactly to match any argument.

func (MatchAny) Match added in v1.3.0

func (ma MatchAny) Match(arg any) (b bool)

type MatchType added in v1.3.0

type MatchType[T any] struct{}

MatchType it's an argument matcher that matches any value that's the same type as provided. Use it on CalledWith or CalledWithExactly to match an argument by type.

func (MatchType[T]) Match added in v1.3.0

func (ma MatchType[T]) Match(arg any) bool

type Mock

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

Mock represents a mock and its use information

func NewMock

func NewMock() Mock

NewMock returns a new mock struct

func (*Mock) Assert added in v1.3.1

func (mock *Mock) Assert(t *testing.T) *mockAssertion

func (*Mock) Called

func (mock *Mock) Called() bool

Called returns if the mock was called

func (*Mock) CalledOnce

func (mock *Mock) CalledOnce() bool

CalledOnce returns if a mock was called exactly once

func (*Mock) CalledTimes

func (mock *Mock) CalledTimes(n int) bool

CalledTimes returns if a mock was called 'n' times

func (*Mock) CalledWith

func (mock *Mock) CalledWith(args ...any) bool

CalledWith returns if the mock was called at least once with the specified arguments

func (*Mock) CalledWithExactly

func (mock *Mock) CalledWithExactly(args ...any) bool

CalledWithExactly returns if the mock was called at least once with exactly the specified arguments, with the same values and in the same order

func (*Mock) GetCalls

func (mock *Mock) GetCalls() []MockCall

GetCalls returns the mock calls

func (*Mock) GetMethodResponse

func (mock *Mock) GetMethodResponse(methodName string, args ...any) (res methodResponse)

GetMethodResponse gets the specified response for a method

func (*Mock) GetResponseAndRegister added in v1.2.0

func (mock *Mock) GetResponseAndRegister(methodName string, args ...any) (res methodResponse)

GetResponseAndRegister it's equivalent of calling RegisterMethodCall and GetMethodResponse subsequently.

It gets the specified response for a method, given the method name and the args, and also registers a method call given those args.

func (*Mock) Method

func (mock *Mock) Method(name string) *method

Method filters the mock use information for a specific method

func (*Mock) RegisterMethodCall

func (mock *Mock) RegisterMethodCall(methodName string, args ...any)

RegisterMethodCall registers a method call on a mock given the method name and the call arguments

func (*Mock) Reset

func (mock *Mock) Reset()

Reset resets a mock to an empty state

func (*Mock) SetMethodResponse

func (mock *Mock) SetMethodResponse(methodName string, response ...any)

SetMethodResponse sets a response that the mock will return when calling the method specified in the methodName

Its imperative that the response values specified are of the same type and are in the same order as the method response specified in the method signature

type MockCall

type MockCall struct {
	MethodName string
	Args       []any
}

MockCall represents a mock call, with the call arguments

func (*MockCall) HasArgument

func (mc *MockCall) HasArgument(arg any) bool

HasArgument returns if a mock call arguments contains a specific argument

Jump to

Keyboard shortcuts

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