如何在c#中从反序列化的JsonArray中获取值?



我有一个模型类c#如下所示:

public class Persons {
public String Name {get; set;}
public int Age {get; set;}
public String InsuranceId {get; set;}
}

我正在反序列化API的一部分

{
"persons": [
{
"name": "SteveBruns",
"age": 24,
"insuranceId": "M2409891"
},
{
"name": "JohnStash",
"age": 34,
"insuranceId": "N2789012"
}
]
}

必须验证所有名称insuranceId字段,在我的SpecFlow项目。假设我们有一个如下所示的特征文件:

Feature: To hit a GET endpoint and fetch the person details
@Feature-106
Scenario: Fetch the details and validate insurance Id
Given I have a valid token
When I create a GET request for /api/insuranceDetails
Then I should validate <name1>, <name2>, <insuranceId1> and <insuranceId2> fields
Examples:
|name1      |insuranceId1 |name2     |insuranceId2|
|SteveBruns |M2409891     |JohnStash |N2789012    |

这些是步骤定义

[Given(@"I have a valid token")]
public void method() {}
[When(@"I have a valid token")]
public void method() {}
[Then(@"I have a valid token")]
public void method(String name1, String insureanceId1, String name2, String insureanceId2) 
{
// some code here

var details = obj.Persons; // obj is an object of some other class where Person is residing in it

// I have tried the following
// I want to extend this for name2 and insuranceId2
// In a nut shell, in the same testcase, I have to validate both names and insuranceId keys inside Persons JSONArray!

foreach (item in details) 
{
item.Name.Should().Be(name1)
item.InsuranceId.Should().Be(insuranceId1)
}

// P.S: tried this approach instead of using above foreach
List<String> names = new List<String>();
names.add(name1);
names.add(name2);

List<String> insuranceIds = new List<String>();
insuranceIds.add(insuranceId1);
insuranceIds.add(insuranceId2);

for (int i = 0; i < details.size(); i++) 
{
String expectedName = details[i].Name;
String expectedInsruanceId = details[i].InsuranceId;

String actualName = names[i];
String actualInsuranceId = insuranceIds[i];

expectedName.Should().Be(actualName);
expectedInsruanceId.Should().Be(actualInsuranceId );
}
}

实现这一目标的最佳方法是什么?使用lambda (select)?如果有其他建议,请在这里帮助我!

Then步骤中使用水平数据表并利用表帮助器应该可以达到目的。另外,我不认为示例表有什么用(除非这是该场景的一个简短示例)。

Feature: To hit a GET endpoint and fetch the person details
@Feature-106
Scenario: Fetch the details and validate insurance Id
Given I have a valid token
When I create a GET request for /api/insuranceDetails
Then the insurance details should be:
| Name       | Insurance Id |
| SteveBruns | M2409891     |
| JohnStash  | N2789012     |

步骤定义非常简单,假设SpecFlow数据表的列标头与JSON结果中每个Person对象的属性名相匹配:

[@Then(@"Then the insurance details should be:")]
public void ThenTheInsuranceDetailsShouldBe(Table table)
{
var details = obj.Persons; // obj is an object of some other class where Person is residing in it
// if the order of the collection does not matter
table.CompareToSet(details);
// if order matters
table.CompareToSet(details, true);
}

既然您的客户想要使用行为驱动开发,请考虑修改您的步骤以使用他们的业务语言:

@Feature-106
Scenario: Fetch the details and validate insurance Id
Given I have a valid token
When I request the insurance details
Then the insurance details should be:
| Name       | Insurance Id |
| SteveBruns | M2409891     |
| JohnStash  | N2789012     |

端点和它是GET请求的事实是在BDD场景中不需要沟通的实现细节。首先关注业务流程。

相关内容

  • 没有找到相关文章

最新更新