【问题标题】:Strongly-Typed values in specflow scenarioSpecflow 场景中的强类型值
【发布时间】:2019-03-07 14:18:21
【问题描述】:

有没有办法在场景的示例表中使用强类型值? (或替代解决方案)

我想知道我是否已经在编码期间(而不是在运行测试期间)在 userType 列中输入了错字。

更新

file.feature

Scenario Outline: Scenario123
Given Create new user of type "<userType>"
Examples:
| userType     |
| PlatinumUser |
| CommonUser   |

steps.cs

[Given(@"Create new user of type ""(.*)""")]
public void CreateNewUser(UserTypeEnum userType)
{
    // some code like e.g.:
    MyUser user = new MyUser(userType);
    //...
}

enum UserTypeEnum  { CommonUser, PlatinumUser, Spectre }

【问题讨论】:

  • 这个例子看起来有点太模糊了。我不清楚您希望如何将表映射到强类型值。
  • 您能否在大纲中发布更多步骤?这些值是如何使用的?它们的类型将在测试执行时由使用它们的步骤确定。

标签: specflow feature-file


【解决方案1】:

看起来像你所追求的 StepArgumentTransformation?

https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions

按照以下方式使用:

[Binding]
public class Transforms
{
    [StepArgumentTransformation]
    public UserTypeEnum UserTypeTransform(string UserType)
    {
      // return the string converted into the required Enum
    }
}

步骤绑定将看到它需要一个 UserTypeEnum 作为参数,因此它将在任何具有 Binding 属性的类中搜索所有可用的步骤参数转换,并使用此方法执行转换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2018-10-08
    相关资源
    最近更新 更多