PACT .NET 消费者测试:灵活长度数组

Kev*_*fer 5 c# contract consumer pact pact-net

我正在使用 pactNet 来测试一个 API,它应该返回一个灵活长度的数组。

如果我调用“myApi/items/”,它应该返回一个消费者不知道确切尺寸的项目列表。所以答案应该是这样的:

    [
        {
            "id": "1",
            "description": "foo"
        },
        {
            "id": "2",
            "description": "foo2"
        },
        {
            "id": "3",
            "description": "foo3"
        }
    ]
Run Code Online (Sandbox Code Playgroud)

或这个:

    [
        {
            "id": "4",
            "description": "foo4"
        },
        {
            "id": "2",
            "description": "foo2"
        }
    ]
Run Code Online (Sandbox Code Playgroud)

如何为此交互创建合同?

文档是在Ruby中的例子,但是我无法找到C#中的等价物。

我正在使用 pactNet 2.1.1 版。

编辑:这是一个示例,它应该是什么样子。我想知道的是如何声明主体应该包含一个长度灵活的项目数组。

[Test]
    public void GetAllItems()
    {
        //Arrange
        _mockProviderService
            .Given("There are items")
            .UponReceiving("A GET request to retrieve the items")
            .With(new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path = "/items/",
                Headers = new Dictionary<string, object>
                {
                    { "Accept", "application/json" }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status = 200,
                Headers = new Dictionary<string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = // array of items with some attributes
                       // (somthing like: {"id": "2", "description": "foo"}) 
                       // with flexible length
            });

        var consumer = new ItemApiClient(_mockProviderServiceBaseUri);

        //Act
        var result = consumer.GetItems();

        //Assert
        Assert.AreEqual(true, result.Count > 0);

        _mockProviderService.VerifyInteractions();

        data.Dispose();
    }
Run Code Online (Sandbox Code Playgroud)

小智 6

听起来您正在寻找 MinTypeMatcher。

身体部分看起来像下面这样:

Body = Match.MinType(new { id: "1", description: "foo" }, 1)
Run Code Online (Sandbox Code Playgroud)