Skip to content

Mocking object with methods named the same as testify's ones #1717

Answered by brackendawson
frux asked this question in Q&A

You must be logged in to vote

Use obj.Mock.On to access the overidden On method:

package kata_test

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
)

type MyMockedObj struct {
	mock.Mock
}

func (obj *MyMockedObj) On() error {
	arguments := obj.Called()

	return arguments.Error(0)
}

func TestThis(t *testing.T) {
	obj := &MyMockedObj{}
	defer obj.AssertExpectations(t)
	obj.Mock.On("On").Return(nil)
	err := obj.On()
	assert.NoError(t, err)
}

Replies: 1 comment

You must be logged in to vote
0 replies
Answer selected by frux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1715 on March 23, 2025 17:45.