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 關注
- 116 瀏覽
添加回答
舉報