【问题标题】:Specflow - Could we pass parameters in the Specflow Example Tablespecflow - 我们可以在specflow示例表中传递参数
【发布时间】:2020-04-03 10:45:36
【问题描述】:

我的场景是

Scenario Outline: Verify if the user can create an response if user posts a request with invalid payload
    Given User had specified name <name> description <description> in create application request
    And User had specified tenantId id <tenantId> in header of createApplication request
    When User sends a create request to application service with invalidPayload
    Then User should get an error code <errorCode> in the createApplication response with error message as <errorMessage>
Examples:
| description| name | errorCode | errorMessage|tenantid|                                                            
| testdescription| DummyApplication| 857 | Application named ${name} already exists for a given tenant | mock|                   

在上面的部分中,我想验证错误消息,但使用 name 参数,其值已在示例表中定义。 我期望作为参数传递的错误消息是, “给定租户已存在名为 'DummyApplication' 的应用程序。”

【问题讨论】:

    标签: c# api automation bdd specflow


    【解决方案1】:

    通常的方法是在拥有变量时存储它,并在以后需要时重新使用它。例如,您可以使用 ScenarioContext 并拥有一个属性。

     private string Name
     {
       get
       {
          return ScenarioContext.Current["Name"].ToString();
       }
       set
       {
          ScenarioContext.Current["Name"] = value;
       }
            }
    
    [Given(@"User had specified name (.*) description (.*) in create application request")]
            public void GivenUserHadSpecifiedNameDummyApplicationDescriptionTestdescriptionInCreateApplicationRequest(string name, string description)
            {
                // store the name
                this.Name = name;
            }
    
    
            [Then(@"User should get an error code (.*) in the createApplication response with error message as (.*)")]
            public void ThenUserShouldGetAnErrorCodeInTheCreateApplicationResponseWithErrorMessageAsApplicationNamedNameAlreadyExistsForAGivenTenant(int errorCode, string errorMessage)
            {
                // retrieve the name and use
                var expected = errorMessage.Replace("${name}", $"'{this.Name}'");
                //Application named 'DummyApplication' already exists for a given tenant
    
            }
    

    【讨论】:

    • 在当前 specflow 示例表 | description| name | errorCode | errorMessage|tenantid| | testdescription| DummyApplication| 857 | Application named ${name} already exists for a given tenant | mock| 中,如果上面的 name 参数将来更改为 ApplicationName,我是否也不需要将 errormessage 参数从 ${name} 更改为 ${ApplicationName}在步骤定义代码var expected = errorMessage.Replace("${name}", $"'{this.Name}'"); 在上面的代码中,我需要将 ${name} 替换为 ${ApplicationName}
    • 对不起,我刚刚重读了这篇文章。如果您的意思是将 specflow 定义更改为 |description|ApplicationName|... - 在这种情况下,您无需更改任何内容,specflow 步骤需要 2 个参数 - 一个用于名称,一个用于描述。他们叫什么并不重要。 specflow 错误消息示例中的 $(name) 只是一个占位符,它会被替换。
    猜你喜欢
    • 2019-12-09
    • 2021-08-13
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多