1 回答

TA贡献1875条经验 获得超5个赞
StateHandlers不直接更改响应(这可能会影响测试的有效性),它们的存在是为了修改当前测试的提供者的内部状态。使用状态名称(以及可选的参数)来确定应该配置什么状态。
当测试执行时,提供者应该在适当的状态下执行其通常的代码,并做出相应的响应。
StateHandlers: StateHandlers{
"A product with id 1 exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
// modify internal state of the provider, so that product with ID 1 exists in the database
return response, nil
},
"A product with id 2 doesn't exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
// modify internal state of the provider, so that product with ID 2 does not exist in the database
},
},
存储库中有示例,例如https://github.com/pact-foundation/pact-go/blob/master/examples/mux/provider/user_service_test.go#L94-L120。
状态是抽象的——它并不暗示状态是如何配置的。它可以通过更新数据库或配置存根等多种方式实现状态转换。
- 1 回答
- 0 关注
- 133 浏览
添加回答
举报