2017-02-12 16 views
1

に、私は、出力アサートはJSON出力のためのC#Specflow

検証のためのコードを取得するAPIを検証するためにspecflowにテストを書いていますが

[Then(@"the customer details will be returned")] 
    public void ThenTheCustomerDetailsWillBeReturned() 
    { 
     var actualResponse = ScenarioContextWrapper.Response; 
     JObject jsonResult = new JObject(); 
     jsonResult = JObject.Parse(actualResponse); 
     Assert.AreEqual("ABC008", jsonResult.GetType().GetProperty("CustomerCode").GetValue(jsonResult, null)); 
     Assert.AreEqual("ABC Industry", jsonResult.GetType().GetProperty("CustomerName").GetValue(jsonResult, null)); 

    } 

である。しかし、「{」は、ランタイムを実行できませんように私は例外を取得していますヌル参照 "}"上のバインディング

APIの出力は

   {{ 
    "Pagination": { 
"NumberOfItems": 1, 
"PageSize": 200, 
"PageNumber": 1, 
"NumberOfPages": 1 
}, 
"Items": [ 
{ 
    "Addresses": [], 
    "CustomerCode": "ABC008", 
    "CustomerName": "ABC Industry", 
    "GSTVATNumber": null, 
    "BankName": null, 
    "BankBranch": null, 
    "BankAccount": null, 
    "Website": null, 
    "PhoneNumber": null, 
    "FaxNumber": null, 
    "MobileNumber": null, 
    "DDINumber": null, 
    "TollFreeNumber": null, 
    "Email": null, 
    "EmailCC": null, 
    "Currency": { 
    "CurrencyCode": "NZD", 
    "Description": "New Zealand, Dollars", 
    "Guid": "29252c92-3d0e-4eba-a613-f9c6c22ed3a8", 
    "LastModifiedOn": "2017-01-31T20:22:20.816Z" 
    }, 
    "Notes": null, 
    "Taxable": true, 
    "XeroContactId": null, 
    "SalesPerson": null, 
    "DiscountRate": null, 
    "PrintPackingSlipInsteadOfInvoice": null, 
    "PrintInvoice": null, 
    "StopCredit": false, 
    "Obsolete": false, 
    "XeroSalesAccount": null, 
    "XeroCostOfGoodsAccount": null, 
    "SellPriceTier": "", 
    "SellPriceTierReference": null, 
    "CustomerType": "", 
    "PaymentTerm": "", 
    "ContactFirstName": null, 
    "ContactLastName": null, 
    "SourceId": null, 
    "CreatedBy": "[email protected]", 
    "CreatedOn": "2017-02-05T18:50:53.697Z", 
    "Guid": "15145a60-8688-48a5-b849-ab66da3c0288", 
    "LastModifiedOn": "2017-02-05T18:50:53.697Z" 
} 
] 
}} 

あるご質問の元が一杯になっていないので、誰かがcustomercodeためのアサートに

おかげ

+0

は、プロパティ名を確認してください。おそらくそれは理解するのに役立ちます:jsonResult.GetType()。GetProperties()。ToList()。ForEach(x => Console.WriteLine(x.Name)); – KernelMode

+0

@KernelModeはい、それはプロパティ名を持っていません、なぜわかりません、どうすればjson出力をアサートできますか? –

+0

私は答えの例を – KernelMode

答えて

1

を助けてください私は、一例として、短いJSONを使用:

 string actualResponse = "{\"Items\":[{\"CustomerCode\": \"ABC008\", \"TestBla\":\"Bla\"}]}"; 
     JObject jsonResult = JObject.Parse(actualResponse); 

     // Get Null exception. Property does not exist. 
     //Object value = jsonResult.GetType().GetProperty("Items").GetValue(jsonResult, null); 

     // Will work 
     var items = jsonResult["Items"]; 

     // To assert CustomerCode: 
     string value = jsonResult["Items"][0]["CustomerCode"].Value<string>(); 
     Assert.AreEqual("ABC008", value); 

GetPropertyラスの性質。あなたの場合、クラスはJObjectです。

あなたがあなた自身のクラス(のはResultを言わせて)、あなたはそれ独自のプロパティを使用することができます(のはItemを言わせて)にオブジェクトを変換する場合:

Result result = jsonResult.ToObject<Result>(); 
    var items = result.Items 
+0

に追加しました。返信いただきありがとうございます。私はこのコーディングの世界では非常に新しいので、まだ回避しようとしています。値をアサートする方法を少し詳しく説明できますか? –

関連する問題