mirror of
https://github.com/stretchr/testify.git
synced 2025-05-28 18:22:34 +00:00
Improve mock.MatchedBy failed comparison Diff message
This commit is contained in:
parent
a9de4f065a
commit
e209ca88af
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user