Skip to content

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

Description

@brackendawson

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugpkg-mockAny issues related to Mock

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions