本文介绍了PACT .NET消费者测试:灵活的长度数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果我叫"myApi/items/",它应该返回消费者不知道其确切尺寸的物品清单.因此答案应如下所示:

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"
        }
    ]

或者这个:

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

如何为此互动创建合同?

How do I create the contract for this interaction?

文档中,是Ruby中的示例,但我找不到等效的示例在C#中.

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

我正在使用pactNet 2.1.1版.

I am using pactNet version 2.1.1.

这是一个示例,其外观应为.我想知道的是如何声明主体应包含长度灵活的项目数组.

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

推荐答案

听起来像是在寻找MinTypeMatcher.

Sounds like you are looking for the MinTypeMatcher.

身体部位如下所示:

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

这篇关于PACT .NET消费者测试:灵活的长度数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:18