[英]Pact-python : Consumer pacts for POST calls are failing
我正在嘗試使用 pact-python 庫為 POST 端點創建消費者方協議。 但它失敗並顯示“缺少請求”的錯誤。
這不是進行 POST API 調用的客戶端代碼
def create_user(request):
return requests.post("http://localhost:1234/user", data=request).json()
這是我創建消費者協議的測試 class。
class TestUserConsumer(unittest.TestCase):
def test_user_creation(self):
request = {
"name": "Micky",
"age": 0
}
response = {
"id": 1232,
"name": "Micky",
"age": 0
}
pact = Consumer("user_client").has_pact_with(Provider("user_server"))
pact.start_service()
pact.with_request(
method='post',
path='/user',
body=request
).will_respond_with(status=200, body=response)
with pact:
create_user(request)
pact.verify()
pact.stop_service()
測試失敗,出現以下錯誤。
line 268, in verify
assert resp.status_code == 200, resp.text
AssertionError: Actual interactions do not match expected interactions for mock MockService.
Missing requests:
POST /user
create_user(request) 正在執行,但交互仍然沒有記錄在 pact 模擬服務器上。
注意: GET API 協議創建工作正常。 只有 POST 失敗。
感謝幫助。
我解決了這個問題。 在提出請求之前,我沒有將我的字典轉換為 json。 因此請求正文格式被錯誤地發送。 這在驗證協議時導致模擬服務器失敗。
我還注意到,最初沒有生成日志。 這是由於我在停止服務器之前添加的斷言。 由於斷言失敗,協議模擬服務器沒有停止。 因此根本不會生成日志。 一旦我停止服務器,就會添加日志,這有助於我識別問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.