Skip to content

Add mocktest package: Silent TestingT for use inside Eventually#203

Open
lei-wego wants to merge 1 commit into
mainfrom
chore/add-mocktest
Open

Add mocktest package: Silent TestingT for use inside Eventually#203
lei-wego wants to merge 1 commit into
mainfrom
chore/add-mocktest

Conversation

@lei-wego
Copy link
Copy Markdown
Collaborator

Summary

Adds a tiny new module `github.com/wego/pkg/mocktest` with a single export — `mocktest.Silent` — a `mock.TestingT` implementation that discards every call. Use it as the `TestingT` argument to testify mock assertion helpers (`AssertNumberOfCalls`, `AssertCalled`, `AssertNotCalled`, `AssertExpectations`) when polling inside `Eventually`.

Why

testify v1.11.0 changed `assert.Eventually` to evaluate the condition at t=0 before the first tick (stretchr/testify#1424). Mock assertion helpers all call `t.Errorf` on failure — so when called inside `Eventually` with the real test `T`, the initial poll (which runs before any async goroutine has had a chance to fire) permanently marks the test failed even when the condition becomes true on a later tick.

`mocktest.Silent` swallows the per-poll failure, letting the helpers be polled safely:

```go
assert.Eventually(t, func() bool {
return m.AssertNumberOfCalls(mocktest.Silent, "Foo", 1)
}, waitFor, tick)
```

Context

`wego/payments` currently works around this with a `replace github.com/stretchr/testify => v1.10.0` directive (added in wego/payments#1951) to keep ~106 legacy call sites green. Once this PR lands and is tagged, payments will adopt `mocktest.Silent`, refactor those sites, and drop the testify pin.

Tests

  • `Silent` satisfies `mock.TestingT` (compile-time check).
  • 4 runtime tests covering all four assertion helpers (`AssertNumberOfCalls`, `AssertCalled`, `AssertNotCalled`, `AssertExpectations`), verifying that:
    • mismatches don't mark the outer test failed
    • delayed async calls eventually satisfy the condition (the original bug)

Run: `cd mocktest && go test -v .` → 5/5 pass.

Test plan

  • Local `go test -v .` clean
  • Pre-push hook clean
  • Wait for CI

After merge: tag as `mocktest/v0.1.0` so consumers can pin.

…ually

testify v1.11.0 changed assert.Eventually to evaluate the condition at
t=0 before the first tick (stretchr/testify#1424). Mock assertion
helpers (AssertNumberOfCalls, AssertExpectations, AssertCalled,
AssertNotCalled) all call t.Errorf on failure — when called from
inside Eventually with the real test T, the t=0 evaluation runs
before any async goroutine has fired, permanently marking the test
failed even if the condition becomes true on a later tick.

mocktest.Silent is a mock.TestingT that swallows all calls, letting
those helpers be polled safely from Eventually conditions:

    assert.Eventually(t, func() bool {
        return m.AssertNumberOfCalls(mocktest.Silent, "Foo", 1)
    }, waitFor, tick)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants