Documentation ¶
Overview ¶
Package ut implements some testing utilities. So far it includes CallTracker, which helps you build mock implementations of interfaces.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NilOrError ¶
NilOrError is a utility function for returning err from mocked methods
Types ¶
type CallTracker ¶
type CallTracker interface { // AddCall() is used by tests to add an expected call to the tracker AddCall(name string, params ...any) CallTracker // SetReturns() is called immediately after AddCall() to set the return // values for the call. SetReturns(returns ...any) CallTracker // TrackCall() is called within mocks to track a call to the Mock. It // returns the return values registered via SetReturns() TrackCall(name string, params ...any) []any // AssertDone() should be called at the end of a test to confirm all // the expected calls have been made AssertDone() // RecordCall() is called to indicate calls to the named mock method should // be recorded rather than asserted. The parameters to any call to the // named method will be recorded and may be retrieved via GetRecordedParams. // The returns from the method are also specified on this call and must be // the same each time. // Note that the ordering of recorded calls relative to other calls is not // tracked. RecordCall(name string, returns ...any) CallTracker // GetRecordedParams returns the sets of parameters passed to a call captured // via RecordCall GetRecordedParams(name string) ([][]any, bool) }
CallTracker is an interface to help build mocks.
Build the CallTracker interface into your mocks. Use TrackCall within mock methods to track calls to the method and the parameters used. Within tests use AddCall to add expected method calls, and SetReturns to indicate what the calls will return.
The tests for this package contain a full example.
type MyMock struct {ut.CallTracker} func (m *MyMock) AFunction(p int) error { r := m.TrackCall("AFunction", p) return NilOrError(r[0]) } func Something(m Functioner) { m.AFunction(37) } func TestSomething(t *testing.T) { m := &MyMock{NewCallRecords(t)} m.AddCall("AFunction", 37).SetReturns(nil) Something(m) m.AssertDone() }
func NewCallRecords ¶
func NewCallRecords(t testing.TB) CallTracker
NewCallRecords creates a new call tracker
Click to show internal directories.
Click to hide internal directories.