【问题标题】:PACT .NET consumer test: flexible length arrayPACT .NET 消费者测试:灵活长度数组
【发布时间】:2017-11-17 08:36:45
【问题描述】:

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

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

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

或者这个:

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

如何为此交互创建合同?

documentation 是 Ruby 中的一个示例,但我在 C# 中找不到等价物。

我使用的是 pactNet 2.1.1 版。

编辑:这是一个示例。我想知道的是如何声明 body 应该包含一个长度灵活的项目数组。

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

【问题讨论】:

    标签: c# consumer contract pact pact-net


    【解决方案1】:

    听起来您正在寻找 MinTypeMatcher。

    身体部分如下所示:

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

    【讨论】:

    • 谢谢!这正是我想要的。
    【解决方案2】:

    对于像@SlimSjakie 这样的嵌套数组场景,只需在具有数组的属性处重复 Match.MinType。例如,以下 Person 对象的 Address 属性为数组。

    Person = Match.MinType(new
    {
        FirstName = Match.Type("string"),
        LastName = Match.Type("string"),
        Address = Match.MinType(new
        {
            Street = Match.Type("string"),
            Town = Match.Type("string"),
            State = Match.Type("string"),
            Postcode = Match.Type("string"),
            Country = Match.Type("string")
        }, 1)
    }, 1)
    

    【讨论】:

      【解决方案3】:

      那么,如果我想匹配具有列表或数组属性的对象的类型呢?

      如果您执行 Match.Type(new myType()) 并且数组或列表属性中的元素数量不完全相同,则测试将失败。你会看到这样的错误:

      Description of differences
      --------------------------------------
      * Actual array is too long and should not contain a Hash at $.data.itinerary[2]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-22
        • 1970-01-01
        • 2020-10-22
        相关资源
        最近更新 更多