首页 > 解决方案 > 如何使用 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#specflow

解决方案


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


推荐阅读