簡體   English   中英

無法通過Testify Mock對象錯誤

[英]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帶參數的方法12傳遞給它,你還要指定調用應該返回整數值5

但是相反

assert.Equal(t, 5, result.Add(5, 6))

您正在調用帶有參數56 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM