MCPcopy
hub / github.com/stretchr/testify / MethodCalled

Method MethodCalled

mock/mock.go:498–588  ·  view source on GitHub ↗

MethodCalled tells the mock object that the given method has been called, and gets an array of arguments to return. Panics if the call is unexpected (i.e. not preceded by appropriate .On .Return() calls) If Call.WaitFor is set, blocks until the channel is closed or receives a message.

(methodName string, arguments ...interface{})

Source from the content-addressed store, hash-verified

496// by appropriate .On .Return() calls)
497// If Call.WaitFor is set, blocks until the channel is closed or receives a message.
498func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Arguments {
499 m.mutex.Lock()
500 // TODO: could combine expected and closes in single loop
501 found, call := m.findExpectedCall(methodName, arguments...)
502
503 if found < 0 {
504 // expected call found, but it has already been called with repeatable times
505 if call != nil {
506 m.mutex.Unlock()
507 m.fail("\nassert: mock: The method has been called over %d times.\n\tEither do one more Mock.On(%#v).Return(...), or remove extra call.\n\tThis call was unexpected:\n\t\t%s\n\tat: %s", call.totalCalls, methodName, callString(methodName, arguments, true), assert.CallerInfo())
508 }
509 // we have to fail here - because we don't know what to do
510 // as the return arguments. This is because:
511 //
512 // a) this is a totally unexpected call to this method,
513 // b) the arguments are not what was expected, or
514 // c) the developer has forgotten to add an accompanying On...Return pair.
515 closestCall, mismatch := m.findClosestCall(methodName, arguments...)
516 m.mutex.Unlock()
517
518 if closestCall != nil {
519 m.fail("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\nDiff: %s\nat: %s\n",
520 callString(methodName, arguments, true),
521 callString(methodName, closestCall.Arguments, true),
522 diffArguments(closestCall.Arguments, arguments),
523 strings.TrimSpace(mismatch),
524 assert.CallerInfo(),
525 )
526 } else {
527 m.fail("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(%#v).Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo())
528 }
529 }
530
531 for _, requirement := range call.requires {
532 if satisfied, _ := requirement.Parent.checkExpectation(requirement); !satisfied {
533 m.mutex.Unlock()
534 m.fail("mock: Unexpected Method Call\n-----------------------------\n\n%s\n\nMust not be called before%s:\n\n%s",
535 callString(call.Method, call.Arguments, true),
536 func() (s string) {
537 if requirement.totalCalls > 0 {
538 s = " another call of"
539 }
540 if call.Parent != requirement.Parent {
541 s += " method from another mock instance"
542 }
543 return
544 }(),
545 callString(requirement.Method, requirement.Arguments, true),
546 )
547 }
548 }
549
550 if call.Repeatability == 1 {
551 call.Repeatability = -1
552 } else if call.Repeatability > 1 {
553 call.Repeatability--
554 }
555 call.totalCalls++

Callers 5

CalledMethod · 0.95
Test_MockMethodCalledFunction · 0.80

Calls 8

findExpectedCallMethod · 0.95
failMethod · 0.95
findClosestCallMethod · 0.95
CallerInfoFunction · 0.92
callStringFunction · 0.85
diffArgumentsFunction · 0.85
newCallFunction · 0.85
checkExpectationMethod · 0.80