mock

package
v0.43.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package mock provides a simple way to stub struct methods.

Index

Constants

This section is empty.

Variables

View Source
var Any = struct{}{}

Any input

Functions

func Proxy

func Proxy[M any](mock Fallbackable, method M) M

Proxy the input and output of method on mock for later stub.

func Stub

func Stub[M any](mock Fallbackable, method M, stub M)

Stub the method with stub

Types

type Call added in v0.38.1

type Call struct {
	Input  []any
	Return []any
}

Call record the input and output of a method call

type Fallbackable

type Fallbackable interface {
	Fallback(any)
}

Fallbackable interface

type Mock

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

Mock helper for interface stubbing

func (*Mock) Calls added in v0.38.1

func (m *Mock) Calls(method any) []Call

Calls returns all the calls of method

func (*Mock) Fallback

func (m *Mock) Fallback(fb interface{})

Fallback the methods that are not stubbed to fb.

func (*Mock) Stop

func (m *Mock) Stop(method any)

Stop the stub

type StubOn

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

StubOn utils

func On

func On[M any](mock Fallbackable, method M) *StubOn

On helper to stub methods to conditionally return values.

func (*StubOn) When

func (s *StubOn) When(in ...interface{}) *StubWhen

When input args of stubbed method matches in

type StubReturn

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

StubReturn utils

func (*StubReturn) Once

func (s *StubReturn) Once() *StubOn

Once specifies stubs only once before stop

func (*StubReturn) Times

func (s *StubReturn) Times(n int) *StubOn

Times specifies how how many stubs before stop, if n <= 0 it will never stop.

type StubTimes

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

StubTimes utils

type StubWhen

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

StubWhen utils

func (*StubWhen) Count

func (s *StubWhen) Count() int

Count returns how many times this condition has been matched

func (*StubWhen) Return

func (s *StubWhen) Return(out ...interface{}) *StubReturn

Return the out as the return values of stubbed method

Jump to

Keyboard shortcuts

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