double

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const SpyTestingTRecordIgnoreParam = spyTestingTRecordIgnoreParam(42)

SpyTestingTRecordIgnoreParam is a sentinel value used in expected records to indicate that a particular parameter should be ignored during comparison. This is useful for testing calls with parameters that are unpredictable or irrelevant.

Example:

spy.ExpectRecords(t, true, SpyTestingTRecord{
	Method: "Logf",
	Inputs: []any{"Error: %v", SpyTestingTRecordIgnoreParam}, // Don't care about the exact error
})

Variables

This section is empty.

Functions

This section is empty.

Types

type Fake

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

Fake implements a minimal TestingT that does nothing. It's useful for tests that need a TestingT but don't need its real behavior.

func NewFake

func NewFake(opts ...FakeOption) *Fake

NewFake creates a new Fake test double.

func (Fake) Cleanup

func (t Fake) Cleanup(f func())

Cleanup implements the TestingT interface. Registers a function to be called when the test completes.

func (Fake) Context

func (t Fake) Context() context.Context

Context implements the TestingT interface. Returns the context specified during creation, or background context by default.

func (Fake) Deadline

func (t Fake) Deadline() (time.Time, bool)

Deadline implements the TestingT interface. Returns the deadline specified during creation, or no deadline by default.

func (Fake) Fail

func (Fake) Fail()

Fail implements the TestingT interface. This is a no-op implementation.

func (Fake) FailNow

func (Fake) FailNow()

FailNow implements the TestingT interface. This is a no-op implementation.

func (Fake) Helper

func (Fake) Helper()

Helper implements the TestingT interface. This is a no-op implementation.

func (Fake) Log added in v1.0.2

func (Fake) Log(...any)

Log implements the TestingT interface. This is a no-op implementation.

func (Fake) Logf

func (Fake) Logf(string, ...any)

Logf implements the TestingT interface. This is a no-op implementation.

type FakeOption

type FakeOption func(o *fakeOptions)

FakeOption is a function that configures a Fake instance. It follows the functional options pattern for configuring the Fake test double.

func FakeWithContext

func FakeWithContext(ctx context.Context) FakeOption

FakeWithContext sets a specific context for a Fake. This replaces the default background context returned by Context().

func FakeWithDeadline

func FakeWithDeadline(deadline time.Time) FakeOption

FakeWithDeadline sets a specific deadline for a Fake. This affects the Deadline() method, which will return this deadline and true.

func FakeWithRegisterCleanup

func FakeWithRegisterCleanup(f func(func())) FakeOption

FakeWithRegisterCleanup configures the cleanup registration function for a Fake. This allows tests to capture or control the behavior of cleanup registrations.

type Spy

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

Spy implements the TestingT interface and records all method calls for later verification. It's useful for testing assertions and other test utilities by wrapping another TestingT implementation and intercepting all method calls.

func NewSpy

func NewSpy(underlyingT TestingT) *Spy

NewSpy creates a new Spy that wraps the provided TestingT implementation. All method calls on the returned Spy will be recorded and also delegated to the underlying TestingT instance.

This allows test code to verify the behavior of code that uses a TestingT without failing the actual test unless explicitly checked.

func (*Spy) Cleanup

func (spy *Spy) Cleanup(cleanupFunc func())

Cleanup implements the TestingT interface.

func (*Spy) Context

func (spy *Spy) Context() context.Context

Context implements the TestingT interface.

func (*Spy) Deadline

func (spy *Spy) Deadline() (time.Time, bool)

Deadline implements the TestingT interface.

func (*Spy) ExpectLogsToContain

func (spy *Spy) ExpectLogsToContain(t TestingT, expect string, more ...string)

ExpectLogsToContain verifies that all the provided strings are contained within the spy's logs. Fails the test if any of the strings are not found in the concatenated logs. This is useful for verifying that specific messages were logged during the test.

func (*Spy) ExpectNoLogs

func (spy *Spy) ExpectNoLogs(t TestingT)

ExpectNoLogs verifies that no logs were captured by the spy. Fails the test if any logs were captured. This is useful for ensuring that no messages were logged during the test.

func (*Spy) ExpectRecords

func (spy *Spy) ExpectRecords(t TestingT, strict bool, expected ...SpyTestingTRecord)

ExpectRecords verifies that the spy contains the expected method call records.

Two matching modes are supported:

  • Strict mode (strict=true): The actual records must exactly match the expected records in the same order.
  • Non-strict mode (strict=false): The expected records can appear in any order within the actual records, and additional unexpected records are allowed.

In non-strict mode, all expected records must still exist in the actual records, but:

  • The order doesn't matter (records can be matched in any order)
  • Extra records in the spy beyond what's expected are ignored
  • All expected records must be present in the actual records

The method fails the test if the verification doesn't pass.

func (*Spy) ExpectTestToFail

func (spy *Spy) ExpectTestToFail(t TestingT)

ExpectTestToFail verifies that the test failed. Fails the test if no failure was recorded. This is useful for testing assertion functions that should fail tests.

func (*Spy) ExpectTestToPass

func (spy *Spy) ExpectTestToPass(t TestingT)

ExpectTestToPass verifies that the test passed. Fails the test if any failure was recorded. This is useful for testing assertion functions that should pass tests.

func (*Spy) Fail

func (spy *Spy) Fail()

Fail implements the TestingT interface.

func (*Spy) FailNow

func (spy *Spy) FailNow()

FailNow implements the TestingT interface. Warning: the goroutine is not stopped.

func (*Spy) Helper

func (spy *Spy) Helper()

Helper implements the TestingT interface.

func (*Spy) Log added in v1.0.2

func (spy *Spy) Log(args ...any)

Log implements the TestingT interface.

func (*Spy) Logf

func (spy *Spy) Logf(format string, args ...any)

Logf implements the TestingT interface.

type SpyTestingTRecord

type SpyTestingTRecord struct {
	Method  string // Name of the method called
	Inputs  []any  // Arguments passed to the method (if any)
	Outputs []any  // Return values from the method (if any)
}

SpyTestingTRecord represents a single method call recorded by Spy. It captures the method name along with its inputs and outputs.

type TestingT

type TestingT = internal.TestingT

TestingT is an interface for testing types, mirroring the standard library's *testing.T.

Jump to

Keyboard shortcuts

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