如何向我现有的 Specflow 测试添加另一个参数?

How to add another parameter to my existing Specflow test?

我目前有一个工作测试,可以验证授权和禁止的 API 响应(401 和 403 响应)。

我现在想在现有测试中添加一个成功参数,而不必创建另一个单独的测试。但我不确定我将如何合并它。所以基本上我会将响应代码作为成功 (200) 添加到示例中,但我不确定我会将 headerCondition 设置为什么。此外,为了在我的代码中获得成功 (200),我需要传递键 'token-auth' 和值 'ToBeProvided'。我需要将其包含在我的 When 步骤中,如下所示。

Scenario Outline: Authenticating endpoint
When I request transaction notification endpoint with headers 
<HeaderCondition>
Then I get a response <ResponseCode>
Examples:
| ResponseCode | HeaderCondition |
| Unauthorized | false           |
| Forbidden    | true            |

[When(@"I request notification endpoint with headers (.*)")]
      public void 
WhenIRequestNotificationEndpointWithHeaders(string headerCondition)
    {
        var baseurl = "(My end point)";
        var client = new RestClient(baseurl);
        var request = new RestRequest(Method.PUT);

        if (headerCondition.Equals("true"))
        {
            request.AddHeader("Ocp-Apim-Subscription-Key", "b601454182cf47eba7ahfjuejdksiwhfjmd");
            request.AddHeader("Content-Type", "application/json");
            //request.AddJsonBody("{\"Id\":\"123\"}");
            request.AddParameter("undefined", "{\"Id\":\"123\"}", ParameterType.RequestBody);
        }

        response = client.Execute(request);
    }

您可以为 When 步骤添加一个新的布尔参数,或者将您已有的布尔值(从技术上讲,您已将其设为字符串)更改为表示枚举的字符串。

多一个布尔值 (IsSendingToken) 的示例:

Scenario Outline: Authenticating endpoint
When I request transaction notification endpoint with headers <HeaderCondition> and sending token <IsSendingToken>
Then I get a response <ResponseCode>

Examples:
| ResponseCode | HeaderCondition | IsSendingToken |
| Unauthorized | false           | false          |
| Forbidden    | true            | false          |
| Ok           | true            | true           |

[When(@"I request notification endpoint with headers (.*) and sending token (true|false)")]
public void WhenIRequestNotificationEndpointWithHeaders(string headerCondition, bool isSendingToken)
    {
        var baseurl = "(My end point)";
        var client = new RestClient(baseurl);
        var request = new RestRequest(Method.PUT);

        if (headerCondition.Equals("true"))
        {
            request.AddHeader("Ocp-Apim-Subscription-Key", "b601454182cf47eba7ahfjuejdksiwhfjmd");
            request.AddHeader("Content-Type", "application/json");
            //request.AddJsonBody("{\"Id\":\"123\"}");
            request.AddParameter("undefined", "{\"Id\":\"123\"}", ParameterType.RequestBody);
        }

        if (isSendingToken == true)
        {
            request.AddHeader("token-auth", "ToBeProvided");
        }

        response = client.Execute(request);
    }

将 HeaderCondition 更改为枚举(作为字符串)的示例:

Scenario Outline: Authenticating endpoint
When I request transaction notification endpoint with headers <HeaderCondition>
Then I get a response <ResponseCode>

Examples:
| ResponseCode | HeaderCondition |
| Unauthorized | Unauthorized    |
| Forbidden    | Forbidden       |
| Ok           | Ok              |

[When(@"I request notification endpoint with headers (.*)")]
public void WhenIRequestNotificationEndpointWithHeaders(string headerCondition)
    {
        var baseurl = "(My end point)";
        var client = new RestClient(baseurl);
        var request = new RestRequest(Method.PUT);

        if (headerCondition.Equals("Forbidden"))
        {
            request.AddHeader("Ocp-Apim-Subscription-Key", "b601454182cf47eba7ahfjuejdksiwhfjmd");
            request.AddHeader("Content-Type", "application/json");
            //request.AddJsonBody("{\"Id\":\"123\"}");
            request.AddParameter("undefined", "{\"Id\":\"123\"}", ParameterType.RequestBody);
        }
        else if(headerCondition.Equals("Ok"))
        {
            request.AddHeader("Ocp-Apim-Subscription-Key", "b601454182cf47eba7ahfjuejdksiwhfjmd");
            request.AddHeader("Content-Type", "application/json");
            //request.AddJsonBody("{\"Id\":\"123\"}");
            request.AddParameter("undefined", "{\"Id\":\"123\"}", ParameterType.RequestBody);
            request.AddHeader("token-auth", "ToBeProvided");
        }

        response = client.Execute(request);
    }

我为 HeaderCondition 重新使用了 ResponseCode 值。 HeaderCondition 可以是您喜欢的任何内容,只要它们在功能文件和步骤文件中匹配即可。您还可以使用 ResponseCode 作为 When 步骤的输入而不是 headerCondition。