Skip to content

Commit a8e9b32

Browse files
committed
capture and assert test failure
1 parent 1b039ee commit a8e9b32

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

mock_test.go

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,18 +368,68 @@ func Test_MultipleTraps(t *testing.T) {
368368
}
369369

370370
func Test_UnreleasedCalls(t *testing.T) {
371-
t.Skip("this test is meant to demonstrate how unreleased calls fail")
372371
t.Parallel()
373-
testCtx, testCancel := context.WithTimeout(context.Background(), 10*time.Second)
374-
defer testCancel()
375-
mClock := quartz.NewMock(t)
372+
tRunFail(t, func(t testing.TB) {
373+
testCtx, testCancel := context.WithTimeout(context.Background(), 10*time.Second)
374+
defer testCancel()
375+
mClock := quartz.NewMock(t)
376376

377-
trap := mClock.Trap().Now()
378-
defer trap.Close()
377+
trap := mClock.Trap().Now()
378+
defer trap.Close()
379379

380-
go func() {
381-
_ = mClock.Now()
382-
}()
380+
go func() {
381+
_ = mClock.Now()
382+
}()
383+
384+
trap.MustWait(testCtx) // missing release
385+
})
386+
}
387+
388+
type captureFailTB struct {
389+
failed bool
390+
testing.TB
391+
}
392+
393+
func (t *captureFailTB) Errorf(format string, args ...any) {
394+
t.Helper()
395+
t.Logf(format, args...)
396+
t.failed = true
397+
}
383398

384-
trap.MustWait(testCtx) // missing release
399+
func (t *captureFailTB) Error(args ...any) {
400+
t.Helper()
401+
t.Log(args...)
402+
t.failed = true
403+
}
404+
405+
func (t *captureFailTB) Fatal(args ...any) {
406+
t.Helper()
407+
t.Log(args...)
408+
t.failed = true
409+
}
410+
411+
func (t *captureFailTB) Fatalf(format string, args ...any) {
412+
t.Helper()
413+
t.Logf(format, args...)
414+
t.failed = true
415+
}
416+
417+
func (t *captureFailTB) Fail() {
418+
t.failed = true
419+
}
420+
421+
func (t *captureFailTB) FailNow() {
422+
t.failed = true
423+
}
424+
425+
func (t *captureFailTB) Failed() bool {
426+
return t.failed
427+
}
428+
429+
func tRunFail(t testing.TB, f func(t testing.TB)) {
430+
tb := &captureFailTB{TB: t}
431+
f(tb)
432+
if !tb.Failed() {
433+
t.Fatal("want test to fail")
434+
}
385435
}

0 commit comments

Comments
 (0)