From f5c86b95bd0a397faf92a37f0873d7019b65d54c Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 31 Oct 2025 12:10:21 +0000 Subject: [PATCH] fix: improve trap logging --- mock.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mock.go b/mock.go index d70f245..feecea5 100644 --- a/mock.go +++ b/mock.go @@ -738,6 +738,10 @@ type Trap struct { unreleasedCalls int } +func (t *Trap) String() string { + return fmt.Sprintf("%s(..., %v)", t.fn.String(), t.tags) +} + func (t *Trap) catch(c *apiCall) { select { case t.calls <- c: @@ -763,12 +767,13 @@ func (t *Trap) Close() { defer t.mock.mu.Unlock() select { case <-t.done: + t.mock.tb.Logf("trap %s already Closed()", t) return // already closed default: } if t.unreleasedCalls != 0 { t.mock.tb.Helper() - t.mock.tb.Errorf("trap Closed() with %d unreleased calls", t.unreleasedCalls) + t.mock.tb.Errorf("trap %s Closed() with %d unreleased calls", t, t.unreleasedCalls) } for i, tr := range t.mock.traps { if t == tr { @@ -814,7 +819,7 @@ func (t *Trap) MustWait(ctx context.Context) *Call { t.mock.tb.Helper() c, err := t.Wait(ctx) if err != nil { - t.mock.tb.Fatalf("context expired while waiting for trap: %s", err.Error()) + t.mock.tb.Fatalf("context expired while waiting for trap %s: %s", t, err.Error()) } return c }