繁体   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