@@ -249,27 +249,25 @@ func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *
249
249
return - 1 , nil
250
250
}
251
251
252
- func (m * Mock ) findClosestCall (method string , arguments ... interface {}) (bool , * Call ) {
253
- diffCount := 0
252
+ func (m * Mock ) findClosestCall (method string , arguments ... interface {}) (* Call , string ) {
253
+ var diffCount int
254
254
var closestCall * Call
255
+ var err string
255
256
256
257
for _ , call := range m .expectedCalls () {
257
258
if call .Method == method {
258
259
259
- _ , tempDiffCount := call .Arguments .Diff (arguments )
260
+ errInfo , tempDiffCount := call .Arguments .Diff (arguments )
260
261
if tempDiffCount < diffCount || diffCount == 0 {
261
262
diffCount = tempDiffCount
262
263
closestCall = call
264
+ err = errInfo
263
265
}
264
266
265
267
}
266
268
}
267
269
268
- if closestCall == nil {
269
- return false , nil
270
- }
271
-
272
- return true , closestCall
270
+ return closestCall , err
273
271
}
274
272
275
273
func callString (method string , arguments Arguments , includeArgumentValues bool ) string {
@@ -316,6 +314,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
316
314
// If Call.WaitFor is set, blocks until the channel is closed or receives a message.
317
315
func (m * Mock ) MethodCalled (methodName string , arguments ... interface {}) Arguments {
318
316
m .mutex .Lock ()
317
+ //TODO: could combine expected and closes in single loop
319
318
found , call := m .findExpectedCall (methodName , arguments ... )
320
319
321
320
if found < 0 {
@@ -326,11 +325,16 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
326
325
// b) the arguments are not what was expected, or
327
326
// c) the developer has forgotten to add an accompanying On...Return pair.
328
327
329
- closestFound , closestCall := m .findClosestCall (methodName , arguments ... )
328
+ closestCall , mismatch := m .findClosestCall (methodName , arguments ... )
330
329
m .mutex .Unlock ()
331
330
332
- if closestFound {
333
- panic (fmt .Sprintf ("\n \n mock: Unexpected Method Call\n -----------------------------\n \n %s\n \n The closest call I have is: \n \n %s\n \n %s\n " , callString (methodName , arguments , true ), callString (methodName , closestCall .Arguments , true ), diffArguments (closestCall .Arguments , arguments )))
331
+ if closestCall != nil {
332
+ panic (fmt .Sprintf ("\n \n mock: Unexpected Method Call\n -----------------------------\n \n %s\n \n The closest call I have is: \n \n %s\n \n %s\n Diff: %s" ,
333
+ callString (methodName , arguments , true ),
334
+ callString (methodName , closestCall .Arguments , true ),
335
+ diffArguments (closestCall .Arguments , arguments ),
336
+ strings .TrimSpace (mismatch )),
337
+ )
334
338
} else {
335
339
panic (fmt .Sprintf ("\n assert: mock: I don't know what to return because the method call was unexpected.\n \t Either do Mock.On(\" %s\" ).Return(...) first, or remove the %s() call.\n \t This method was unexpected:\n \t \t %s\n \t at: %s" , methodName , methodName , callString (methodName , arguments , true ), assert .CallerInfo ()))
336
340
}
@@ -627,6 +631,7 @@ func (args Arguments) Is(objects ...interface{}) bool {
627
631
//
628
632
// Returns the diff string and number of differences found.
629
633
func (args Arguments ) Diff (objects []interface {}) (string , int ) {
634
+ //TODO: could return string as error and nil for No difference
630
635
631
636
var output = "\n "
632
637
var differences int
0 commit comments