相关疑难解决方法(0)

Gomock 使用不同的输入两次相同的方法

注意:不是使用 testify - 不同的库两次使用不同的输入和输出来重复模拟接口方法。

我正在使用该github.com/golang/mock/gomock库来模拟 HTTP 客户端接口,以测试代码的行为。Post()我的代码在客户端上使用相同的方法两次,但针对两个不同的端点。

我试过:

mockUc.EXPECT().
    Post("m-elasticsearch/_sql/translate", gomock.Eq(expectedQuery), gomock.Any(), gomock.Any()).
    SetArg(2, esQuery).
    Return(http.StatusOK, nil).
    Times(1)
mockUc.EXPECT().
    Post("m-elasticsearch/app-*/_search", gomock.Eq(esQuery), gomock.Any(), gomock.Any()).
    SetArg(2, logResults).
    Return(http.StatusOK, nil).
    Times(1)
Run Code Online (Sandbox Code Playgroud)

但这给了我错误,告诉我EXPECT()在第一次调用时正在考虑第二个:

 expected call at [...] doesn't match the argument at index 0.
        Got: m-elasticsearch/_sql/translate (string)
        Want: is equal to m-elasticsearch/app-*/_search (string)
Run Code Online (Sandbox Code Playgroud)

然后我尝试gomock.InOrder()像这样使用:

 expected call at [...] doesn't match the argument at index 0.
        Got: m-elasticsearch/_sql/translate (string)
        Want: is equal to m-elasticsearch/app-*/_search (string)
Run Code Online (Sandbox Code Playgroud)

但这也没有帮助。 …

unit-testing mocking go gomock

3
推荐指数
1
解决办法
7672
查看次数

如何在golang中模拟函数

我写了一个简单的包,它基本上由很多getter函数组成.此包中的每个文件对应一个服务,因此例如产品文件包含与产品服务/ db相关的函数,订单文件以订购服务等.每个函数将db资源作为参数作为参数,并将参数作为参数sql,例如.productid,name,orderid.每个函数都返回一个结构(例如Order,product)或错误:

// product.go

package lib

type Product struct {
   ID int
   Name string
   Price float
}

func GetProductById(DB *sql.DB, ID int) (p Product, err error) {
   q := "SELECT * FROM product WHERE id = " + ID
   ...
}

func GetProductByName(DB *sql.DB, name string) (p Product, err error) {
   ...
}

// order.go

package lib

type Order struct {
   ID int
   Date string
   Items []items
}

func GetOrderById(DB *sql.DB, ID int) (o Order, err error) {
   ...
} …
Run Code Online (Sandbox Code Playgroud)

mocking go

1
推荐指数
1
解决办法
3560
查看次数

标签 统计

go ×2

mocking ×2

gomock ×1

unit-testing ×1