【问题标题】:Pact-python : Consumer pacts for POST calls are failingPact-python:POST 调用的消费者协议失败
【发布时间】:2021-02-26 08:06:29
【问题描述】:

我正在尝试使用 pact-python 库为 POST 端点创建消费者方协议。但它失败并显示“缺少请求”的错误。

这里不是调用 POST API 的客户端代码

def create_user(request):
     return requests.post("http://localhost:1234/user", data=request).json()

这是我创建消费者协议的测试类。

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 失败。

感谢您的帮助。

【问题讨论】:

    标签: python-3.x pact pact-python


    【解决方案1】:

    我发现了问题所在。在发出请求之前,我没有将我的字典转换为 json。因此请求正文格式被错误地发送。这在验证协议时导致模拟服务器失败。

    我还注意到,最初没有生成日志。这是由于我在停止服务器之前添加的断言。由于断言失败,协议模拟服务器没有停止。因此根本不会生成日志。一旦我停止了服务器,就会添加日志,这有助于我识别问题。

    【讨论】:

      猜你喜欢
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 2017-10-16
      相关资源
      最近更新 更多