Skip to content

Mock.IsMethodCallable doesn't work with dynamic expected arguments #1712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
brackendawson opened this issue Mar 22, 2025 · 2 comments · May be fixed by #1718
Open

Mock.IsMethodCallable doesn't work with dynamic expected arguments #1712

brackendawson opened this issue Mar 22, 2025 · 2 comments · May be fixed by #1718
Assignees
Labels

Comments

@brackendawson
Copy link
Collaborator

Mock.IsMethodCallable implements its own incomplete expected argument checking. So it doesn't always work. It should be using the same methods as Mock.Called to check if it is callable.

It also has no reason to accept a TestingT, it doesn't use it at all, there is no point calling Helper, which also isn't part of TestingT.

I also see no sensible use case for IsMethodCallable.

Description

If the expected Args of a Call in Mock.ExpectedCalls are dynamic (mock.Anything, mock.MatchedBy, etc...) then Mock.IsMethodCallable will return the wrong answer.

Step To Reproduce

package kata_test

import (
	"testing"

	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
)

type MyMock struct {
	mock.Mock
}

func TestEventuallyFast(t *testing.T) {
	m := &MyMock{}
	m.On("MyMethod", 1).Return()
	m.On("MyMethod", mock.Anything).Return()
	require.True(t, m.IsMethodCallable(t, "MyMethod", 1))
	require.True(t, m.IsMethodCallable(t, "MyMethod", 2))
}

Expected behavior

Testcase passes

Actual behavior

m.IsMethodCallable(t, "MyMethod", 2) incorrectly returns false.

@brackendawson
Copy link
Collaborator Author

Also it's amusing that a method which does not exist is "Callable"?

@gopherjit
Copy link

@brackendawson please assign this issue to me.

@gopherjit gopherjit linked a pull request Mar 25, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants