2017-11-17 12 views
2

柔軟な長さの配列を返すAPIをテストするためにpactNetを使用しています。PACT .NET消費者テスト:フレキシブルな長さの配列

私が "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を使用しています。

編集:これはどのように見えるかの例です。私が知りたいのは、ボディに柔軟な長さのアイテムの配列を含める必要があると宣言する方法です。

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

答えて

3

あなたがMinTypeMatcherを探しているような感じです。

ボディ部分は、以下のようになります:

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

ありがとうございました!それは私が探していたものです。 –