c#Specflow中Json输出的断言
我正在specflow中编写测试来验证API get输出 验证的代码是c#Specflow中Json输出的断言,c#,api,specflow,C#,Api,Specflow,我正在specflow中编写测试来验证API get输出 验证的代码是 [Then(@"the customer details will be returned")] public void ThenTheCustomerDetailsWillBeReturned () { var actualResponse = ScenarioContextWrapper.Response; JObject jsonResult = new JObject
[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": "qa+applicant@tyt.com",
"CreatedOn": "2017-02-05T18:50:53.697Z",
"Guid": "15145a60-8688-48a5-b849-ab66da3c0288",
"LastModifiedOn": "2017-02-05T18:50:53.697Z"
}
]
}}
有人能帮我确认一下客户代码吗
谢谢我使用了一个简短的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);
检查属性名称。也许它会帮助您理解:jsonResult.GetType().GetProperties().ToList().ForEach(x=>Console.WriteLine(x.Name))@KernelMode是的,它没有属性名称,不知道为什么,我如何断言json输出?我在回答中添加了示例非常感谢你的回答,我对这个编码世界非常陌生,仍在努力解决它,你能详细解释一下它如何有助于断言值吗。
Result result = jsonResult.ToObject<Result>();
var items = result.Items