亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Go 中的 Pact 返回錯誤請求 (400, 500)?

如何使用 Go 中的 Pact 返回錯誤請求 (400, 500)?

Go
蕭十郎 2022-12-05 16:41:04
我正在我的公司采用 Pact,但在 Golang 上,我們在基本情況下遇到了障礙,即消費者作為一個端點的 2 個狀態:Given("存在 id 為 1 的產品").Given("ID 為 2 的產品不存在").我們的麻煩在于不存在的情況。消費者mockProvider.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.VerifyProvider(t, VerifyRequest{        ProviderBaseURL:            "http://localhost:8080",        Provider:                   "ms.pact-provider-example-for-go",        ProviderVersion:            "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 ProviderStateV3) (ProviderStateV3Response, error) {                …                return response, nil            },            "A product with id 2 doesn't exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {                // ???            },        },    })    require.NoError(t, err)}問題我們如何像ProviderStateV3Response地圖界面一樣返回錯誤的請求響應?
查看完整描述

1 回答

?
慕田峪4524236

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。


狀態是抽象的——它并不暗示狀態是如何配置的。它可以通過更新數據庫或配置存根等多種方式實現狀態轉換。


查看完整回答
反對 回復 2022-12-05
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號