在 c# Specflow 中断言 Json 输出
Assert for Json Output in c# Specflow
我正在 specflow 中编写测试以验证 API 获取输出
验证码是
[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));
}
但我收到异常“{"Cannot perform runtime binding on a null reference"}”。
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);
GetProperty
得到 class 属性。在你的例子中 class 是 JObject.
如果您将对象转换为您自己的 class(比方说 Result
),那么您可以使用它自己的属性(比方说 Item
):
Result result = jsonResult.ToObject<Result>();
var items = result.Items
我正在 specflow 中编写测试以验证 API 获取输出
验证码是
[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));
}
但我收到异常“{"Cannot perform runtime binding on a null reference"}”。
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);
GetProperty
得到 class 属性。在你的例子中 class 是 JObject.
如果您将对象转换为您自己的 class(比方说 Result
),那么您可以使用它自己的属性(比方说 Item
):
Result result = jsonResult.ToObject<Result>();
var items = result.Items