Hi I'm trying to mock a struct in GO. I'm using testify to do this. But I can't seem to get it to work and don't now what I'm doing wrong. Below is the sample main.go and main_test.go file I have
// Arithmetic ...
type Arithmetic interface {
Add(int, int) int
Subtract(int, int) int
}
// MathOperation ...
type MathOperation struct {}
// GetNewArithmetic ...
func GetNewArithmetic(obj Arithmetic) Arithmetic {
if obj != nil {
return obj
}
return MathOperation{}
}
// Add ...
func (a MathOperation) Add(num1 int, num2 int) int {
return num1 + num2
}
// Subtract ...
func (a MathOperation) Subtract(num1 int, num2 int) int {
return num1 - num2
}
And here is my test file
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type MyMock struct {
mock.Mock
}
func (m *MyMock) Add(num1 int, num2 int) int {
args := m.Called(num1, num2)
return args.Int(0) + args.Int(1)
}
func (m *MyMock) Subtract(num1 int, num2 int) int {
args := m.Called(num1, num2)
return args.Int(0) + args.Int(1)
}
func TestDoComputation(t *testing.T) {
testobj := new(MyMock)
testobj.On("Add", 1, 2).Return(5)
// a := GetNewArithmetic(testobj)
result := GetNewArithmetic(testobj)
assert.Equal(t, 5, result.Add(5, 6))
testobj.AssertExpectations(t)
}
I receive this error
--- FAIL: TestDoComputation (0.00s)
panic:
mock: Unexpected Method Call
-----------------------------
Add(int,int)
0: 5
1: 6
The closest call I have is:
Add(int,int)
0: 1
1: 2
[recovered]
panic:
mock: Unexpected Method Call
-----------------------------
Add(int,int)
0: 5
1: 6
The closest call I have is:
Add(int,int)
0: 1
1: 2
goroutine 13 [running]:
testing.tRunner.func1(0xc420106870)
/usr/lib/golang/src/testing/testing.go:711 +0x2d2
panic(0x701160, 0xc420011070)
I have no idea on how to fix since this is my first time using Go and using Testify to do unit testing. Would appreciate if someone can take a look and have a working version of this. Thanks