无法通过Testify Mock对象错误

时间:2018-06-09 08:00:58

标签: 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
}

这是我的测试文件

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进行单元测试。如果有人可以看看并拥有一个有效的版本,我将不胜感激。感谢

2 个答案:

答案 0 :(得分:1)

在作证/模拟包裹中

  

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)

答案 1 :(得分:0)

该行

testobj.On("Add", 1, 2).Return(5)

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

但是在这一行

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

您使用参数Add5调用方法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.

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

你可以这样做的方法是使用从Called方法调用返回的args值,这个值将保持Return方法的参数索引的顺序与它们传递的顺序相同进入Return

因此,您在此行传递给5的整数值Return

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)
}
相关问题