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

Method AssertExpectations

mock/mock.go:622–649  ·  view source on GitHub ↗

AssertExpectations asserts that everything specified with On and Return was in fact called as expected. Calls may have occurred in any order.

(t TestingT)

Source from the content-addressed store, hash-verified

620// AssertExpectations asserts that everything specified with On and Return was
621// in fact called as expected. Calls may have occurred in any order.
622func (m *Mock) AssertExpectations(t TestingT) bool {
623 if s, ok := t.(interface{ Skipped() bool }); ok && s.Skipped() {
624 return true
625 }
626 if h, ok := t.(tHelper); ok {
627 h.Helper()
628 }
629
630 m.mutex.Lock()
631 defer m.mutex.Unlock()
632 var failedExpectations int
633
634 // iterate through each expectation
635 expectedCalls := m.expectedCalls()
636 for _, expectedCall := range expectedCalls {
637 satisfied, reason := m.checkExpectation(expectedCall)
638 if !satisfied {
639 failedExpectations++
640 t.Logf(reason)
641 }
642 }
643
644 if failedExpectations != 0 {
645 t.Errorf("FAIL: %d out of %d expectation(s) were met.\n\tThe code you are testing needs to make %d more call(s).\n\tat: %s", len(expectedCalls)-failedExpectations, len(expectedCalls), failedExpectations, assert.CallerInfo())
646 }
647
648 return failedExpectations == 0
649}
650
651func (m *Mock) checkExpectation(call *Call) (bool, string) {
652 if !call.optional && !m.methodWasCalled(call.Method, call.Arguments) && call.totalCalls == 0 {

Calls 6

expectedCallsMethod · 0.95
checkExpectationMethod · 0.95
CallerInfoFunction · 0.92
HelperMethod · 0.65
LogfMethod · 0.65
ErrorfMethod · 0.65