Bug: Unexpected method calls are "forgotten"
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- go
- Domain
- testing-qa
Research direction
Start with the reproducer in the issue and inspect mock/mock.go, especially Mock.MethodCalled and AssertExpectations. Reproduce the suppressed panic, then trace how unexpected calls are recorded or reported. Done means the unexpected call still panics and is also surfaced by AssertExpectations without breaking normal mock behavior.
Written by the indexing model from the issue text.
Description
In a nutshell: this test passes, despite the expectations being incorrect:
https://go.dev/play/p/2r54p3TxXqr
package durable
import (
"testing"
"github.com/stretchr/testify/mock"
)
type mymock struct {
mock.Mock
}
func (m *mymock) Run() error {
args := m.Called()
return args.Error(0)
}
func TestPanics(t *testing.T) {
m := new(mymock)
run := func() {
defer func() { recover() } () // intentionally suppress panic
m.On("Run").Return(nil).Once()
m.Run()
m.Run() // panics
t.Fatal("Unreachable")
}
run()
m.AssertExpectations(t) // passes
}
Granted, this isn't great code. But panic-capturing code is somewhat common in frameworks, and this can lead to tests passing despite clearly having incorrect mocks. The fact that AssertExpectations doesn't match behavior seems like a bug in testify.
So far as I can tell, this is caused by this code below, in mock/mock.go.
Because the panic occurs before the call.totalCalls++ op (and possibly others!), no record of a panic-ing call exists except for the panic.
func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Arguments {
m.mutex.Lock()
found, call := m.findExpectedCall(methodName, arguments...)
if found < 0 {
// we have to fail here - because we don't know what to do
// as the return arguments. This is because:
//
// a) this is a totally unexpected call to this method,
// b) the arguments are not what was expected, or
// c) the developer has forgotten to add an accompanying On...Return pair.
closestFound, closestCall := m.findClosestCall(methodName, arguments...)
m.mutex.Unlock()
if closestFound {
panic(fmt.Sprintf("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\n", callString(methodName, arguments, true), callString(methodName, closestCall.Arguments, true), diffArguments(closestCall.Arguments, arguments)))
} else {
panic(fmt.Sprintf("\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()))
}
}
if call.Repeatability == 1 {
call.Repeatability = -1
} else if call.Repeatability > 1 {
call.Repeatability--
}
call.totalCalls++
// ...
}
I don't know what all would be involved in correcting this though. Ideally this should both panic (which generally aborts the test prematurely) and record the error for reporting in AssertExpectations in case it's suppressed.
- Dominant language
- Go
- Stars
- 26.2k
- Forks
- 1.9k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stretchr/testify
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
internal/cleanup
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
All issues in stretchr/testify
Similar issues
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
blinklabs-io/bursa#904 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 comments ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100