c# - 如何使用 C# 将同一对象模型的多个节点添加到 API POST 请求
问题描述
我需要使用 C# 构造一个对微服务的 POST 请求,然后我需要为使用 specflow 和 nunit 从服务获得的响应编写测试。
示例 POST 请求正文如下。这适用于多个客户
{
"documentType": "Consent",
"customers": [
{
"fullName": "Jane Smith",
"custNumber": "7748"
},
{
"fullName": "John Smith",
"custNumber": "4488"
},
{
"fullName": "Jonny Smith",
"custNumber": "2748"
}
],
}
我的 specflow 场景如下所示
Scenario Outline: Validate customer names in consent for joint account holders
Given consent service
And account holder(s) <customername>
When service is called
Then consent has correct customer name
Examples:
| customername |
| "Jane Smith,John Smith,Jonny Smith" |
任何人都可以帮我修改 sepcflow 步骤的代码,以便它可以满足多个客户的场景吗?
[Given(@"account holders ""(.*)""")]
public void GivenAccountHolders(string customername)
{
var scen = ScenarioContext.Current.ScenarioInfo.Title;
if (scen.Contains("individual"))
{
api_reqBody["customers"][0] = new JObject
{
{"fullName", customername},
{"custNumber", "7748"},
};
}
else
{
string[] split = customername.Split(',');
api_reqBody["customers"][0] = new JObject
{
{"fullName", split[0]},
{"custNumber", "7748"},
};
// How should I add the next two customer details
}
}
api_reqBody 在“给予同意服务”步骤中声明
api_reqBody= consentSupport.CreateDefaultConsentBody()
同意支持类将 CreateDefaultConsentBody 方法定义为
public static JObject CreateDefaultConsentBody()
{
DocumentCreateServiceBody ConsentCreateReqBody = new DocumentCreateServiceBody();
ConsentCreateReqBody.customers[0] = new customerModel { fullName = "testname", custNumber = "1111" };
}
DocumentCreateServiceBody 的类有
public class DocumentCreateServiceBody
{
public customerModel[] customers = new customerModel[1];
}
客户模型的类是
public class customerModel
{
public string fullName { get; set; }
public string custNumber{ get; set; }
}
解决方案
改变了我的方法,在给定同意服务的步骤定义中动态启动了客户模型,并解决了问题
推荐阅读
- c# - 当 CheckBox 未选中时,使其无法在特定时间 C#
- linked-list - 链表的第一个元素(例如list[0])是引用头还是第一个节点?
- android - MPAndroidChart 和同一图表上的多条线
- coldfusion - 文件不属于cf项目,请对文件所属的项目应用cf性质
- file - Groovy:当我加载资源文件时,没有这样的文件或目录
- dart - 组合一组 Uint8List
- android - Android gradle 插件更新到 3.3.0 抛出 NullPointerException
- automation - OSI 和 OPC 有什么区别?
- python - pandas:如何总结大小不等的数据集?
- java - 通过代理减慢redis