无法通过 Testify Mock 对象错误

Mad*_*ing 1 go testify

嗨,我正在尝试模拟 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
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试文件

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)
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误

--- 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)
Run Code Online (Sandbox Code Playgroud)

我不知道如何修复,因为这是我第一次使用 Go 并使用 Testify 进行单元测试。如果有人可以看看并拥有此的工作版本,我们将不胜感激。谢谢

mko*_*iva 5

线

testobj.On("Add", 1, 2).Return(5)
Run Code Online (Sandbox Code Playgroud)

意味着您希望testobj模拟接收对其Add方法的调用12传递给它,并且您还指定该调用应返回整数值5

但是在这条线上

assert.Equal(t, 5, result.Add(5, 6))
Run Code Online (Sandbox Code Playgroud)

您正在调用Add带有参数5和的方法6

这导致你得到的错误:

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.
Run Code Online (Sandbox Code Playgroud)

最重要的是,您的模拟实现正在尝试计算并返回值。这不是模拟应该做的。模拟应该返回通过Return方法提供给它的值。

您可以这样做的方法是使用argsCalled方法调用返回的值,该值将保存Return方法的参数,索引的顺序与它们传入的顺序相同Return

所以5Return在这一行传递给的整数值

testobj.On("Add", 1, 2).Return(5)
Run Code Online (Sandbox Code Playgroud)

可以使用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)
}
Run Code Online (Sandbox Code Playgroud)