2017-02-12 62 views
1

我寫在specflow測試來驗證API獲取輸出斷言在C#Specflow JSON輸出

進行驗證的代碼是

[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獲得c lass財產。在你的情況下,課程是JObject

,如果你將對象轉換到自己的類(比方說Result),那麼你可以使用它自己的屬性(比如說Item):

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

非常感謝您的回覆,我對這個編碼世界非常陌生,仍然試圖解決它,請您詳細解釋它如何幫助確定價值。 –