如何使用 C# 将同一对象模型的多个节点添加到 API POST 请求

How can I add multiple nodes of same object model to an API POST request using C#

我需要使用 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 在 "Given consent service" 步骤中声明

api_reqBody= consentSupport.CreateDefaultConsentBody()

consentSupportclass 将 CreateDefaultConsentBody 方法定义为

     public static JObject CreateDefaultConsentBody()
        {
            DocumentCreateServiceBody ConsentCreateReqBody = new DocumentCreateServiceBody();
            ConsentCreateReqBody.customers[0] = new customerModel { fullName = "testname", custNumber = "1111" };
}

DocumentCreateServiceBody 的 class 有

   public class DocumentCreateServiceBody
    {
        public customerModel[] customers = new customerModel[1];
    }

而客户模型的 class 是

  public class customerModel
    {
        public string fullName { get; set; }
        public string custNumber{ get; set; }
    }

改变了我的方法,在 Given 同意服务的步骤定义中动态启动客户模型并解决了问题