mirror of https://github.com/stretchr/testify.git
Merge branch 'develop'
commit
6d9d998d04
31
mock/mock.go
31
mock/mock.go
|
@ -89,6 +89,30 @@ func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (bool,
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Mock) findClosestCall(method string, arguments ...interface{}) (bool, *Call) {
|
||||||
|
|
||||||
|
diffCount := 0
|
||||||
|
var closestCall *Call = nil
|
||||||
|
|
||||||
|
for _, call := range m.ExpectedCalls {
|
||||||
|
if call.Method == method {
|
||||||
|
|
||||||
|
_, tempDiffCount := call.Arguments.Diff(arguments)
|
||||||
|
if tempDiffCount < diffCount || diffCount == 0 {
|
||||||
|
diffCount = tempDiffCount
|
||||||
|
closestCall = &call
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if closestCall == nil {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, closestCall
|
||||||
|
}
|
||||||
|
|
||||||
func callString(method string, arguments Arguments, includeArgumentValues bool) string {
|
func callString(method string, arguments Arguments, includeArgumentValues bool) string {
|
||||||
|
|
||||||
var argValsString string = ""
|
var argValsString string = ""
|
||||||
|
@ -128,9 +152,16 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
|
||||||
// b) the arguments are not what was expected, or
|
// b) the arguments are not what was expected, or
|
||||||
// c) the developer has forgotten to add an accompanying On...Return pair.
|
// c) the developer has forgotten to add an accompanying On...Return pair.
|
||||||
|
|
||||||
|
closestFound, closestCall := m.findClosestCall(functionName, arguments...)
|
||||||
|
|
||||||
|
if closestFound {
|
||||||
|
panic(fmt.Sprintf("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n", callString(functionName, arguments, true), callString(functionName, closestCall.Arguments, true)))
|
||||||
|
} else {
|
||||||
panic(fmt.Sprintf("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", functionName, functionName, callString(functionName, arguments, true), assert.CallerInfo()))
|
panic(fmt.Sprintf("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", functionName, functionName, callString(functionName, arguments, true), assert.CallerInfo()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// add the call
|
// add the call
|
||||||
m.Calls = append(m.Calls, Call{functionName, arguments, make([]interface{}, 0)})
|
m.Calls = append(m.Calls, Call{functionName, arguments, make([]interface{}, 0)})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue