testify icon indicating copy to clipboard operation
testify copied to clipboard

Mock: nothing does match mock.Anything

Open brackendawson opened this issue 2 years ago • 0 comments

Having more arguments in a mock expectation than the interface has does not fail if those expectations match the string (Missing). This is only really confusing if the matcher is fairly generic, like mock.Anything.

Perhaps the value used here should be a private type?

Testify: v1.7.4 Go: go version go1.18 darwin/amd64

package kata

import (
	"testing"

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

type MyMock struct {
	mock.Mock
}

func (t *MyMock) Do() {
	t.Called()
}

func TestFoo(t *testing.T) {
	m := &MyMock{}
	m.Test(t)
	m.On("Do", mock.Anything, mock.AnythingOfType("string"), "(Missing)", mock.MatchedBy(func(s string) bool {
		t.Errorf("%q", s)
		return true
	})).Return()
	m.Do()
	m.AssertExpectations(t)
}
Running tool: /usr/local/go/bin/go test -timeout 30s -coverprofile=/var/folders/06/jjq25lws0cbcxv37ns0hb_mm0000gn/T/vscode-gohOmAs5/go-code-cover github.com/brackendawson/kata -race

--- FAIL: TestFoo (0.00s)
    /Users/alandaws/src/github.com/brackendawson/kata/kata_test.go:21: "(Missing)"
    /Users/alandaws/src/github.com/brackendawson/kata/kata_test.go:21: "(Missing)"
    /Users/alandaws/src/github.com/brackendawson/kata/kata_test.go:25: PASS:	Do(string,mock.AnythingOfTypeArgument,string,mock.argumentMatcher)
FAIL
coverage: 0.0% of statements
FAIL	github.com/brackendawson/kata	0.195s
FAIL

brackendawson avatar Jun 22 '22 14:06 brackendawson