You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using this library indirectly, through vektra/mockery. I have a mock that I use in a goroutine, and I don't expect any calls on it. However, because of a bug in my code, there sometimes is an unexpected calls. This results in a call to t.FailNow(), which causes necessary cleanup to be skipped and the entire test suite to hang.
Looking at MethodCalled(), which is called by Called(), we can see several calls to m.fail():
m.fail("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo())
FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test.
I think both m.fail() and m.MethodCalled() should come with the same warning.
Step To Reproduce
create a goroutine that calls a mock, and performs an unexpected call on the mock, then closes a channel that should cause the main thread to hang until it is closed
greg0ire
changed the title
Calling MethodCalled() in a goroutine can result in a call to t.FailNow()
Calling Called() in a goroutine can result in a call to t.FailNow()
Mar 5, 2025
Description
I'm using this library indirectly, through
vektra/mockery
. I have a mock that I use in a goroutine, and I don't expect any calls on it. However, because of a bug in my code, there sometimes is an unexpected calls. This results in a call tot.FailNow()
, which causes necessary cleanup to be skipped and the entire test suite to hang.Looking at
MethodCalled()
, which is called byCalled()
, we can see several calls tom.fail()
:testify/mock/mock.go
Lines 509 to 524 in 7c367bb
and
fail()
callsT.FailNow()
:testify/mock/mock.go
Line 352 in 7c367bb
Looking at the documentation of
FailNow()
, we can read the following:I think both
m.fail()
andm.MethodCalled()
should come with the same warning.Step To Reproduce
create a goroutine that calls a mock, and performs an unexpected call on the mock, then closes a channel that should cause the main thread to hang until it is closed
Expected behavior
The channel should still get closed.
Actual behavior
The test hangs forever.
The text was updated successfully, but these errors were encountered: