[英]Cannot pass Testify Mock object error
嗨,我正在嘗試在GO中模擬結構。 我正在使用作證。 但是我似乎無法正常工作,現在也不要做錯了。 以下是我擁有的示例main.go和main_test.go文件
// 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
}
這是我的測試文件
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)
}
我收到此錯誤
--- 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)
我不知道如何解決,因為這是我第一次使用Go並使用Testify進行單元測試。 如果有人可以看看並有一個可行的版本,將不勝感激。 謝謝
線
testobj.On("Add", 1, 2).Return(5)
意味着你所期望的testobj
模擬接收到它的調用Add
帶參數的方法1
和2
傳遞給它,你還要指定調用應該返回整數值5
。
但是相反
assert.Equal(t, 5, result.Add(5, 6))
您正在調用帶有參數5
和6
Add
方法。
這導致出現錯誤:
mock: Unexpected Method Call
-----------------------------
Add(int,int)
0: 5
1: 6
// this is result.Add(5, 6), the 0: and 1: are indexes of the actually passed in aguments.
The closest call I have is:
Add(int,int)
0: 1
1: 2
// this is testobj.On("Add", 1, 2), and 0: and 1: are indexes of the expected arguments.
最重要的是,您的模擬實現正在嘗試計算並返回該值。 這不是模擬應該做的。 模擬應改為返回通過Return
方法提供給它的值。
執行此操作的方法是使用Called
方法調用返回的args
值,該值將保存Return
方法的參數索引,該參數的索引順序與傳遞給Return
方法的順序相同。
因此,您傳遞給這行的Return
的整數值5
testobj.On("Add", 1, 2).Return(5)
可以使用Int
實用程序方法並將其傳遞給第0個索引進行訪問。 那就是return args.Int(0)
將返回整數值5
。
因此,您的測試文件應如下所示:
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)
}
func (m *MyMock) Subtract(num1 int, num2 int) int {
args := m.Called(num1, num2)
return args.Int(0)
}
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(1, 2))
testobj.AssertExpectations(t)
}
在作證/模擬包裝中
testobj.On(“ Add”,1,2).Return(5)
在上一行中,它告訴模擬對象,每當我們調用帶有以下參數的Add方法時,它都應返回5。Return用於將結果傳遞給具有給定參數的方法。
assert.Equal(t,5,result.Add(5,6))
在這行代碼中,您要求將預期的結果與函數調用進行匹配。在您指定傳遞1和2時,函數應該返回5,但在Equal語句中傳遞的是值5和6
您所要做的就是用正確的值更改任一行。
testobj.On(“ Add”,5,6).Return(5)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.