Documentation
¶
Index ¶
- Constants
- type Fake
- type FakeOption
- type Spy
- func (spy *Spy) Cleanup(cleanupFunc func())
- func (spy *Spy) Context() context.Context
- func (spy *Spy) Deadline() (time.Time, bool)
- func (spy *Spy) ExpectLogsToContain(t TestingT, expect string, more ...string)
- func (spy *Spy) ExpectNoLogs(t TestingT)
- func (spy *Spy) ExpectRecords(t TestingT, strict bool, expected ...SpyTestingTRecord)
- func (spy *Spy) ExpectTestToFail(t TestingT)
- func (spy *Spy) ExpectTestToPass(t TestingT)
- func (spy *Spy) Fail()
- func (spy *Spy) FailNow()
- func (spy *Spy) Helper()
- func (spy *Spy) Log(args ...any)
- func (spy *Spy) Logf(format string, args ...any)
- type SpyTestingTRecord
- type TestingT
Constants ¶
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 (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 ¶
Context implements the TestingT interface. Returns the context specified during creation, or background context by default.
func (Fake) Deadline ¶
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.
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 ¶
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) ExpectLogsToContain ¶
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 ¶
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 ¶
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 ¶
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) FailNow ¶
func (spy *Spy) FailNow()
FailNow implements the TestingT interface. Warning: the goroutine is not stopped.
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.