Improve mock.MatchedBy failed comparison Diff message

This commit is contained in:
perrydunn 2021-05-04 16:42:39 +01:00 committed by Boyan Soubachov
parent a9de4f065a
commit e209ca88af
2 changed files with 22 additions and 1 deletions

View File

@ -728,7 +728,7 @@ func (f argumentMatcher) Matches(argument interface{}) bool {
}
func (f argumentMatcher) String() string {
return fmt.Sprintf("func(%s) bool", f.fn.Type().In(0).Name())
return fmt.Sprintf("func(%s) bool", f.fn.Type().In(0).String())
}
// MatchedBy can be used to match a mock call based on only certain properties

View File

@ -1482,6 +1482,10 @@ func (s *timer) GetTime(i int) string {
return s.Called(i).Get(0).(string)
}
func (s *timer) GetTimes(times []int) string {
return s.Called(times).Get(0).(string)
}
type tCustomLogger struct {
*testing.T
logs []string
@ -1554,6 +1558,23 @@ func TestArgumentMatcherToPrintMismatch(t *testing.T) {
m.AssertExpectations(t)
}
func TestArgumentMatcherToPrintMismatchWithReferenceType(t *testing.T) {
defer func() {
if r := recover(); r != nil {
matchingExp := regexp.MustCompile(
`\s+mock: Unexpected Method Call\s+-*\s+GetTimes\(\[\]int\)\s+0: \[\]int\{1\}\s+The closest call I have is:\s+GetTimes\(mock.argumentMatcher\)\s+0: mock.argumentMatcher\{.*?\}\s+Diff:.*\(\[\]int=\[1\]\) not matched by func\(\[\]int\) bool`)
assert.Regexp(t, matchingExp, r)
}
}()
m := new(timer)
m.On("GetTimes", MatchedBy(func(_ []int) bool { return false })).Return("SomeTime").Once()
res := m.GetTimes([]int{1})
require.Equal(t, "SomeTime", res)
m.AssertExpectations(t)
}
func TestClosestCallMismatchedArgumentInformationShowsTheClosest(t *testing.T) {
defer func() {
if r := recover(); r != nil {