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
感謝
檢查的屬性名稱。也許它會幫助你理解:jsonResult.GetType()。GetProperties()。ToList()。ForEach(x => Console.WriteLine(x.Name)); – KernelMode
@KernelMode是的,它沒有屬性名稱,不知道爲什麼,我怎麼能斷言JSON輸出? –
我在答案中增加了一個例子 – KernelMode