Search code examples
c#consumercontractpactpact-net

PACT .NET consumer test: flexible length array


I am using pactNet to test an API which should return an array of a flexible length.

If i call "myApi/items/" it should return a list of items where the consumer does not know the exact size of. So the answer should look like this:

    [
        {
            "id": "1",
            "description": "foo"
        },
        {
            "id": "2",
            "description": "foo2"
        },
        {
            "id": "3",
            "description": "foo3"
        }
    ]

or this:

    [
        {
            "id": "4",
            "description": "foo4"
        },
        {
            "id": "2",
            "description": "foo2"
        }
    ]

How do I create the contract for this interaction?

In the documentation is an example in Ruby, but I cannot find the equivalent in C#.

I am using pactNet version 2.1.1.

Edit: Here is an example how it should look like. What I want to know is how do I declare that the body should contain an array of items with a flexible length.

[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();
    }

Solution

  • Sounds like you are looking for the MinTypeMatcher.

    The body part would look something like below:

    Body = Match.MinType(new { id: "1", description: "foo" }, 1)