Gomock同一个方法两次,但输入不同。

huangapple go评论82阅读模式
英文:

Gomock same method twice with different input

问题

注意:这是你的中文翻译结果:

我正在使用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)

但是这也没有帮助。

我在这里尝试的方法是否可行?

英文:

Note: Not a duplicate of https://stackoverflow.com/questions/62165773/mock-interface-method-twice-with-different-input-and-output-using-testify - Different library.

I am using the github.com/golang/mock/gomock library to mock an HTTP client interface in order to test the behaviour of my code. My code uses the same Post() method on the client twice, but for 2 different endpoints.

I tried:

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)

But this gave me the error, telling me that the second EXPECT() was being considered on the first call:

 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)

I then tried using gomock.InOrder() like this:

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)

But this didn't help either.

Is what I'm trying to do possible here?

答案1

得分: 3

代替编写两个EXPECT,你可以使用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
		}
	})
英文:

Instead of writing two EXPECTs, you can use DoAndReturn method and return the value as you want in one EXPECT. I could not write types because I do not know method signature.

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

huangapple
  • 本文由 发表于 2022年5月21日 08:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/72325840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定