简体   繁体   English

无法通过Testify Mock对象错误

[英]Cannot pass Testify Mock object error

Hi I'm trying to mock a struct in GO. 嗨,我正在尝试在GO中模拟结构。 I'm using testify to do this. 我正在使用作证。 But I can't seem to get it to work and don't now what I'm doing wrong. 但是我似乎无法正常工作,现在也不要做错了。 Below is the sample main.go and main_test.go file I have 以下是我拥有的示例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
}

And here is my test file 这是我的测试文件

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)
}

I receive this error 我收到此错误

--- 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)

I have no idea on how to fix since this is my first time using Go and using Testify to do unit testing. 我不知道如何解决,因为这是我第一次使用Go并使用Testify进行单元测试。 Would appreciate if someone can take a look and have a working version of this. 如果有人可以看看并有一个可行的版本,将不胜感激。 Thanks 谢谢

The line 线

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

means that you expect the testobj mock to receive a call to its Add method with arguments 1 and 2 passed to it, and you also specify that that call should return the integer value 5 . 意味着你所期望的testobj模拟接收到它的调用Add带参数的方法12传递给它,你还要指定调用应该返回整数值5

But instead on this line 但是相反

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

you are calling the method Add with arguments 5 and 6 . 您正在调用带有参数56 Add方法。

This results in the error you got: 这导致出现错误:

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.

On top of that your mock implementations are attempting to calculate and return the value. 最重要的是,您的模拟实现正在尝试计算并返回该值。 This is not what a mock should do. 这不是模拟应该做的。 A mock should instead return the value provided to it through the Return method. 模拟应改为返回通过Return方法提供给它的值。

The way you can do this is by using the args value returned from the Called method call, this value will hold the Return method's arguments indexed in the same order they were passed in to Return . 执行此操作的方法是使用Called方法调用返回的args值,该值将保存Return方法的参数索引,该参数的索引顺序与传递给Return方法的顺序相同。

So the integer value 5 that you passed to Return on this line 因此,您传递给这行的Return的整数值5

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

can be accessed using the Int utility method and passing it the 0th index. 可以使用Int实用程序方法并将其传递给第0个索引进行访问。 That is return args.Int(0) will return the interger value 5 . 那就是return args.Int(0)将返回整数值5

So your test file should look more like this: 因此,您的测试文件应如下所示:

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)
}

In testify/mock package 在作证/模拟包装中

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

In above line,It tell the mock object that whenever we Call Add method with following arguments,it should return 5. Return is used to pass the result to the method with the given arguments. 在上一行中,它告诉模拟对象,每当我们调用带有以下参数的Add方法时,它都应返回5。Return用于将结果传递给具有给定参数的方法。

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

In this line,you are asking to match the expected result with the function call.Earlier you specify when you pass 1 & 2,the function should return 5 but in Equal statement you are passing values 5 & 6 在这行代码中,您要求将预期的结果与函数调用进行匹配。在您指定传递1和2时,函数应该返回5,但在Equal语句中传递的是值5和6

All you have to is the change any of the one line with the correct value. 您所要做的就是用正确的值更改任一行。

testobj.On("Add", 5, 6).Return(5) testobj.On(“ Add”,5,6).Return(5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM