注意:不是Mock interface method twice with different input and output using 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)但这给了我错误,告诉我在第一个电话中正在考虑第二个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)然后我试着像这样使用gomock.InOrder():
first := mockUc.EXPECT().
Post("m-elasticsearch/_sql/translate", gomock.Eq(expectedQuery), gomock.Any(), gomock.Any()).
SetArg(2, esQuery).
Return(http.StatusOK, nil).
Times(1)
second := mockUc.EXPECT().
Post("m-elasticsearch/app-*/_search", gomock.Eq(esQuery), gomock.Any(), gomock.Any()).
SetArg(2, logResults).
Return(http.StatusOK, nil).
Times(1)
gomock.InOrder(first, second)但这也无济于事。
我在这里做的事有可能吗?
发布于 2022-05-21 13:44:57
与编写两个期望值不同,您可以使用DoAndReturn方法并在一个EXPECT中返回您想要的值。我无法写入类型,因为我不知道方法签名。
mockUc.
EXPECT().
Post(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(url, query string, ...) (int, error) {
if url == "m-elasticsearch/_sql/translate" {
return http.StatusOK, nil
} else {
return http.StatusOK, nil
}
})https://stackoverflow.com/questions/72325840
复制相似问题