我在我的公司致力于采用Pact,但在Golang上,我们遇到了一个基本问题,即一个消费者作为两个州的一个端点:
- 给定("存在id为1的产品").
- 给定("id为2的产品不存在").
我们的问题在于"不存在"这个案子.
消费者
mock供应商.AddInteraction().
Given("The product with ID 66 doesn't exists").
UponReceiving("a request Product 66").
WithRequest(http.MethodGet, S("/api/v1/product/66")).
WillRespondWith(http.StatusNotFound).
供应商
func TestContract(t *testing.T) {
SetLogLevel("TRACE")
verifier := HTTPVerifier{}
err := verifier.Verify供应商(t, VerifyRequest{
供应商BaseURL: "http://localhost:8080",
供应商: "ms.pact-provider-example-for-go",
供应商Version: "example", // os.Getenv("APP_SHA"),
BrokerURL: "https://…", // os.Getenv("PACT_BROKER_BASE_URL"),
PublishVerificationResults: false,
StateHandlers: StateHandlers{
"A product with id 1 exists": func(setup bool, s 供应商StateV3) (供应商StateV3Response, error) {
…
return response, nil
},
"A product with id 2 doesn't exists": func(setup bool, s 供应商StateV3) (供应商StateV3Response, error) {
// ???
},
},
})
require.NoError(t, err)
}
问题
当供应商StateV3Response
是一个映射接口时,我们如何返回错误的请求响应?