go-mock: github.com/maraino/go-mock Index | Files

package mock

import "github.com/maraino/go-mock"

Package mock provides a mocking framework for Go.

https://github.com/maraino/go-mock

Index

Package Files

mock.go

func AssertVerifyMocks Uses

func AssertVerifyMocks(t HasError, mocks ...HasVerify)

Fail the test if any of the mocks fail verification

func VerifyMocks Uses

func VerifyMocks(mocks ...HasVerify) (bool, error)

VerifyMocks verifies a list of mocks, and returns the first error, if any.

type AnyIfType Uses

type AnyIfType func(interface{}) bool

AnyIfType defines the type used as an argument that satisfies a condition.

func AnyIf Uses

func AnyIf(f func(interface{}) bool) AnyIfType

AnyIf is a helper to define AnyIfType arguments.

Example:

f := func(i interface{}) bool {
	ii, ok := i.(MyType)
	return ok && ii.ID = "the-id"
}
mock.When("MyMethod", mock.AnyIf(f)).Return(0)

func Slice Uses

func Slice(elements ...interface{}) AnyIfType

Slice is a helper to define AnyIfType arguments for slices and their elements.

Example:

mock.When("MyMethod", mock.Slice(123, mock.Rest)).Return(0)

type AnyType Uses

type AnyType string

AnyType defines the type used as a replacement for any kind of argument in the stub configuration.

const (
    Any AnyType = "mock.any"
)

Any is the constant that should be used to represent a AnyType.

Example:

mock.When("MyMethod", mock.Any, mock.Any).Return(0)

type AnythingOfType Uses

type AnythingOfType string

AnythingOfType defines the type used as a replacement for an argument of a specific type in the stub configuration.

func AnyOfType Uses

func AnyOfType(t string) AnythingOfType

AnyOfType is a helper to define AnythingOfType arguments.

Example:

mock.When("MyMethod", mock.AnyOfType("int"), mock.AnyOfType("string")).Return(0)

type HasError Uses

type HasError interface {
    Error(...interface{})
}

Used to represent a test we can fail, without importing the testing package Importing "testing" in a file not named *_test.go results in tons of test.* flags being added to any compiled binary including this package

type HasVerify Uses

type HasVerify interface {
    Verify() (bool, error)
}

HasVerify is used as the input of VerifyMocks (Mock satisfies it, obviously)

type Mock Uses

type Mock struct {
    Functions []*MockFunction
    // contains filtered or unexported fields
}

Mock should be embedded in the struct that we want to act as a Mock.

Example:

type MyClient struct {
	mock.Mock
}

func (*Mock) Called Uses

func (m *Mock) Called(arguments ...interface{}) *MockResult

Called is the function used in the mocks to replace the actual task.

Example:

func (m *MyClient) Request(url string) (int, string, error) {
	r := m.Called(url)
	return r.Int(0), r.String(1), r.Error(2)
}

func (*Mock) Reset Uses

func (m *Mock) Reset() *Mock

Reset removes all stubs defined.

func (*Mock) Verify Uses

func (m *Mock) Verify() (bool, error)

Verify verifies the restrictions set in the stubbing.

func (*Mock) When Uses

func (m *Mock) When(name string, arguments ...interface{}) *MockFunction

When defines an stub of one method with some specific arguments. It returns a *MockFunction that can be configured with Return, ReturnToArgument, Panic, ...

type MockCountCheckType Uses

type MockCountCheckType int
const (
    NONE MockCountCheckType = iota
    TIMES
    AT_LEAST
    AT_MOST
    BETWEEN
)

type MockFunction Uses

type MockFunction struct {
    Name              string
    Arguments         []interface{}
    ReturnValues      []interface{}
    ReturnToArguments []MockReturnToArgument
    PanicValue        interface{}
    // contains filtered or unexported fields
}

MockFunction is the struct used to store the properties of a method stub.

func (*MockFunction) AtLeast Uses

func (f *MockFunction) AtLeast(a int) *MockFunction

AtLeast defines the number of times that a *MockFunction must be at least called. This is verified if mock.Verify is called.

func (*MockFunction) AtMost Uses

func (f *MockFunction) AtMost(a int) *MockFunction

AtMost defines the number of times that a *MockFunction must be at most called. This is verified if mock.Verify is called.

func (*MockFunction) Between Uses

func (f *MockFunction) Between(a, b int) *MockFunction

Between defines a range of times that a *MockFunction must be called. This is verified if mock.Verify is called.

func (*MockFunction) Call Uses

func (f *MockFunction) Call(call interface{}) *MockFunction

Call executes a function passed as an argument using the arguments pased to the stub. If the function returns any output parameters they will be used as a return arguments when the stub is called. If the call argument is not a function it will panic when the stub is executed.

Example:

mock.When("MyMethod", mock.Any, mock.Any).Call(func(a int, b int) int {
	return a+b
})

func (*MockFunction) Panic Uses

func (f *MockFunction) Panic(v interface{}) *MockFunction

Panic defines a panic for a specific *MockFunction.

func (*MockFunction) Return Uses

func (f *MockFunction) Return(v ...interface{}) *MockFunction

Return defines the return values of a *MockFunction.

func (*MockFunction) ReturnToArgument Uses

func (f *MockFunction) ReturnToArgument(n int, v interface{}) *MockFunction

ReturnToArgument defines the values returned to a specific argument of a *MockFunction.

func (*MockFunction) Timeout Uses

func (f *MockFunction) Timeout(d time.Duration) *MockFunction

Timeout defines a timeout to sleep before returning the value of a function.

func (*MockFunction) Times Uses

func (f *MockFunction) Times(a int) *MockFunction

Times defines how many times a *MockFunction must be called. This is verified if mock.Verify is called.

type MockResult Uses

type MockResult struct {
    Result []interface{}
}

MockResult struct is used to store the return arguments of a method stub.

func (*MockResult) Bool Uses

func (r *MockResult) Bool(i int) bool

Bool returns a specific return parameter as a bool. If a result has not been set, it returns false.

func (*MockResult) Byte Uses

func (r *MockResult) Byte(i int) byte

Byte returns a specific return parameter as a byte. If a result has not been set, it returns 0.

func (*MockResult) Bytes Uses

func (r *MockResult) Bytes(i int) []byte

Bytes returns a specific return parameter as a []byte. If a result has not been set, it returns nil.

func (*MockResult) Contains Uses

func (r *MockResult) Contains(i int) bool

Contains returns true if the results have the index i, false otherwise.

func (*MockResult) Error Uses

func (r *MockResult) Error(i int) error

Error returns a specific return parameter as an error. If a result has not been set, it returns nil.

func (*MockResult) Float32 Uses

func (r *MockResult) Float32(i int) float32

Float32 returns a specific return parameter as a float32. If a result has not been set, it returns 0.

func (*MockResult) Float64 Uses

func (r *MockResult) Float64(i int) float64

Float64 returns a specific return parameter as a float64. If a result has not been set, it returns 0.

func (*MockResult) Get Uses

func (r *MockResult) Get(i int) interface{}

Get returns a specific return parameter. If a result has not been set, it returns nil,

func (*MockResult) GetType Uses

func (r *MockResult) GetType(i int, ii interface{}) interface{}

GetType returns a specific return parameter with the same type of the second argument. A nil version of the type can be casted without causing a panic.

func (*MockResult) Int Uses

func (r *MockResult) Int(i int) int

Int returns a specific return parameter as an int. If a result has not been set, it returns 0.

func (*MockResult) Int16 Uses

func (r *MockResult) Int16(i int) int16

Int16 returns a specific return parameter as an int16. If a result has not been set, it returns 0.

func (*MockResult) Int32 Uses

func (r *MockResult) Int32(i int) int32

Int32 returns a specific return parameter as an int32. If a result has not been set, it returns 0.

func (*MockResult) Int64 Uses

func (r *MockResult) Int64(i int) int64

Int64 returns a specific return parameter as an int64. If a result has not been set, it returns 0.

func (*MockResult) Int8 Uses

func (r *MockResult) Int8(i int) int8

Int8 returns a specific return parameter as an int8. If a result has not been set, it returns 0.

func (*MockResult) String Uses

func (r *MockResult) String(i int) string

String returns a specific return parameter as a string. If a result has not been set, it returns "".

type MockReturnToArgument Uses

type MockReturnToArgument struct {
    Argument int
    Value    interface{}
}

MockReturnToArgument defines the function arguments used as return parameters.

type RestType Uses

type RestType string

RestType indicates there may optionally be one or more remaining elements.

const Rest RestType = "mock.rest"

Rest indicates there may optionally be one or more remaining elements.

Example:

mock.When("MyMethod", mock.Slice(123, mock.Rest)).Return(0)

Package mock imports 7 packages (graph) and is imported by 9 packages. Updated 2018-04-05. Refresh now. Tools for package owners.