Skip to content

Commit b89eecf

Browse files
Ernesto Jiménezernesto-jimenez
Ernesto Jiménez
authored andcommitted
Improve errors from mock assertions
Mark the assert helpers as helpers for Go Versions that support `t.Helper()`
1 parent 0bfbef4 commit b89eecf

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

mock/mock.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ type assertExpectationser interface {
382382
//
383383
// Calls may have occurred in any order.
384384
func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
385+
if h, ok := t.(tHelper); ok {
386+
h.Helper()
387+
}
385388
for _, obj := range testObjects {
386389
if m, ok := obj.(Mock); ok {
387390
t.Logf("Deprecated mock.AssertExpectationsForObjects(myMock.Mock) use mock.AssertExpectationsForObjects(myMock)")
@@ -399,6 +402,9 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
399402
// AssertExpectations asserts that everything specified with On and Return was
400403
// in fact called as expected. Calls may have occurred in any order.
401404
func (m *Mock) AssertExpectations(t TestingT) bool {
405+
if h, ok := t.(tHelper); ok {
406+
h.Helper()
407+
}
402408
m.mutex.Lock()
403409
defer m.mutex.Unlock()
404410
var somethingMissing bool
@@ -431,6 +437,9 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
431437

432438
// AssertNumberOfCalls asserts that the method was called expectedCalls times.
433439
func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool {
440+
if h, ok := t.(tHelper); ok {
441+
h.Helper()
442+
}
434443
m.mutex.Lock()
435444
defer m.mutex.Unlock()
436445
var actualCalls int
@@ -445,6 +454,9 @@ func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls
445454
// AssertCalled asserts that the method was called.
446455
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
447456
func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool {
457+
if h, ok := t.(tHelper); ok {
458+
h.Helper()
459+
}
448460
m.mutex.Lock()
449461
defer m.mutex.Unlock()
450462
if !m.methodWasCalled(methodName, arguments) {
@@ -465,6 +477,9 @@ func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interfac
465477
// AssertNotCalled asserts that the method was not called.
466478
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
467479
func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool {
480+
if h, ok := t.(tHelper); ok {
481+
h.Helper()
482+
}
468483
m.mutex.Lock()
469484
defer m.mutex.Unlock()
470485
if m.methodWasCalled(methodName, arguments) {
@@ -679,6 +694,9 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
679694
// Assert compares the arguments with the specified objects and fails if
680695
// they do not exactly match.
681696
func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
697+
if h, ok := t.(tHelper); ok {
698+
h.Helper()
699+
}
682700

683701
// get the differences
684702
diff, diffCount := args.Diff(objects)
@@ -826,3 +844,7 @@ var spewConfig = spew.ConfigState{
826844
DisableCapacities: true,
827845
SortKeys: true,
828846
}
847+
848+
type tHelper interface {
849+
Helper()
850+
}

0 commit comments

Comments
 (0)