Skip to content

Commit e209ca8

Browse files
perrydunnboyan-soubachov
authored andcommitted
Improve mock.MatchedBy failed comparison Diff message
1 parent a9de4f0 commit e209ca8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mock/mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ func (f argumentMatcher) Matches(argument interface{}) bool {
728728
}
729729

730730
func (f argumentMatcher) String() string {
731-
return fmt.Sprintf("func(%s) bool", f.fn.Type().In(0).Name())
731+
return fmt.Sprintf("func(%s) bool", f.fn.Type().In(0).String())
732732
}
733733

734734
// MatchedBy can be used to match a mock call based on only certain properties

mock/mock_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,10 @@ func (s *timer) GetTime(i int) string {
14821482
return s.Called(i).Get(0).(string)
14831483
}
14841484

1485+
func (s *timer) GetTimes(times []int) string {
1486+
return s.Called(times).Get(0).(string)
1487+
}
1488+
14851489
type tCustomLogger struct {
14861490
*testing.T
14871491
logs []string
@@ -1554,6 +1558,23 @@ func TestArgumentMatcherToPrintMismatch(t *testing.T) {
15541558
m.AssertExpectations(t)
15551559
}
15561560

1561+
func TestArgumentMatcherToPrintMismatchWithReferenceType(t *testing.T) {
1562+
defer func() {
1563+
if r := recover(); r != nil {
1564+
matchingExp := regexp.MustCompile(
1565+
`\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`)
1566+
assert.Regexp(t, matchingExp, r)
1567+
}
1568+
}()
1569+
1570+
m := new(timer)
1571+
m.On("GetTimes", MatchedBy(func(_ []int) bool { return false })).Return("SomeTime").Once()
1572+
1573+
res := m.GetTimes([]int{1})
1574+
require.Equal(t, "SomeTime", res)
1575+
m.AssertExpectations(t)
1576+
}
1577+
15571578
func TestClosestCallMismatchedArgumentInformationShowsTheClosest(t *testing.T) {
15581579
defer func() {
15591580
if r := recover(); r != nil {

0 commit comments

Comments
 (0)