Documentation
¶
Overview ¶
Package testing provides test utilities for the transactional outbox pattern.
MockOutbox implements app.OutboxPublisher for unit testing, allowing application developers to verify event publishing without a real database.
Usage:
mockOutbox := outboxtest.NewMockOutbox() svc := NewOrderService(db, mockOutbox) svc.CreateOrder(ctx, order) outboxtest.AssertEventPublished(t, mockOutbox, "order.created")
Index ¶
- func AssertEventCount(t *testing.T, mock *MockOutbox, expected int)
- func AssertEventNotPublished(t *testing.T, mock *MockOutbox, eventType string)
- func AssertEventPublished(t *testing.T, mock *MockOutbox, eventType string)
- func AssertEventTypeCount(t *testing.T, mock *MockOutbox, eventType string, expected int)
- func AssertEventWithAggregate(t *testing.T, mock *MockOutbox, eventType, aggregateID string)
- func AssertEventWithExchange(t *testing.T, mock *MockOutbox, eventType, exchange string)
- func AssertNoEvents(t *testing.T, mock *MockOutbox)
- type MockOutbox
- func (m *MockOutbox) Events() []PublishedEvent
- func (m *MockOutbox) EventsByType(eventType string) []PublishedEvent
- func (m *MockOutbox) Publish(_ context.Context, _ dbtypes.Tx, event *app.OutboxEvent) (string, error)
- func (m *MockOutbox) Reset()
- func (m *MockOutbox) WithError(err error) *MockOutbox
- type PublishedEvent
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertEventCount ¶
func AssertEventCount(t *testing.T, mock *MockOutbox, expected int)
AssertEventCount asserts that exactly the expected number of events were published (all types).
func AssertEventNotPublished ¶
func AssertEventNotPublished(t *testing.T, mock *MockOutbox, eventType string)
AssertEventNotPublished asserts that no events with the given type were published.
func AssertEventPublished ¶
func AssertEventPublished(t *testing.T, mock *MockOutbox, eventType string)
AssertEventPublished asserts that at least one event with the given type was published.
func AssertEventTypeCount ¶
func AssertEventTypeCount(t *testing.T, mock *MockOutbox, eventType string, expected int)
AssertEventTypeCount asserts that exactly the expected number of events with the given type were published.
func AssertEventWithAggregate ¶
func AssertEventWithAggregate(t *testing.T, mock *MockOutbox, eventType, aggregateID string)
AssertEventWithAggregate asserts that an event with the given type and aggregate ID was published.
func AssertEventWithExchange ¶
func AssertEventWithExchange(t *testing.T, mock *MockOutbox, eventType, exchange string)
AssertEventWithExchange asserts that an event with the given type and exchange was published.
func AssertNoEvents ¶
func AssertNoEvents(t *testing.T, mock *MockOutbox)
AssertNoEvents asserts that no events were published.
Types ¶
type MockOutbox ¶
type MockOutbox struct {
// contains filtered or unexported fields
}
MockOutbox implements app.OutboxPublisher for unit testing. It records all published events and supports configurable failures.
Thread-safe: all operations use sync.Mutex for concurrent test scenarios.
func NewMockOutbox ¶
func NewMockOutbox() *MockOutbox
NewMockOutbox creates a new MockOutbox instance with no configured failures.
func (*MockOutbox) Events ¶
func (m *MockOutbox) Events() []PublishedEvent
Events returns a copy of all published events.
func (*MockOutbox) EventsByType ¶
func (m *MockOutbox) EventsByType(eventType string) []PublishedEvent
EventsByType returns all published events matching the given event type.
func (*MockOutbox) Publish ¶
func (m *MockOutbox) Publish(_ context.Context, _ dbtypes.Tx, event *app.OutboxEvent) (string, error)
Publish implements app.OutboxPublisher. Records the event and returns a deterministic event ID ("mock-event-1", "mock-event-2", etc.).
func (*MockOutbox) Reset ¶
func (m *MockOutbox) Reset()
Reset clears all recorded events and resets the ID counter.
func (*MockOutbox) WithError ¶
func (m *MockOutbox) WithError(err error) *MockOutbox
WithError configures the mock to return an error on every Publish() call. This is useful for testing error handling when the outbox is unavailable.
type PublishedEvent ¶
type PublishedEvent struct {
EventID string
Event *app.OutboxEvent
}
PublishedEvent records a single Publish() call for later assertion.