我正在使用 pactNet 来测试一个 API,它应该返回一个灵活长度的数组。
如果我调用“myApi/items/”,它应该返回消费者不知道确切大小的项目列表。所以答案应该是这样的:
[
{
"id": "1",
"description": "foo"
},
{
"id": "2",
"description": "foo2"
},
{
"id": "3",
"description": "foo3"
}
]
或这个:
[
{
"id": "4",
"description": "foo4"
},
{
"id": "2",
"description": "foo2"
}
]
如何为此交互创建合同?
在文档中是 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();
}