Python 3.x Pact python:POST调用的消费者协议失败

Python 3.x Pact python:POST调用的消费者协议失败,python-3.x,pact,pact-python,Python 3.x,Pact,Pact Python,我正在尝试使用pact python库为POST端点创建用户端pact。但是它失败了,错误是“缺少请求” 下面是进行POST API调用的客户端代码 def create_user(request): return requests.post("http://localhost:1234/user", data=request).json() 这是我创建消费者契约的测试类 class TestUserConsumer(unittest.TestCase): de

我正在尝试使用pact python库为POST端点创建用户端pact。但是它失败了,错误是“缺少请求”

下面是进行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_用户(请求)正在被执行,但交互仍然没有记录在pact mock服务器上

注意:GetAPI协议创建正在工作。只有职位失败了


谢谢你的帮助。

我解决了这个问题。在发出请求之前,我没有将字典转换为json。因此,请求正文格式发送不正确。这导致在验证pact时模拟服务器出现故障

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