【问题标题】:Can you pass Generic type <T> to specflow steps?您可以将通用类型 <T> 传递给 specflow 步骤吗?
【发布时间】:2016-02-26 22:49:38
【问题描述】:

在绑定 specflow 测试时,通过 specflow 可以很容易地传递字符串、int、json 文件,但是在干燥的良好实践中,您可以在 specflow 步骤中传递泛型类型吗?

例如

Scenario
When I call the URL with the RequestObject and Type <url> <request> <type>
The response shall equal <response>

|url|request|type                 |
|xyn|abc    |customObjectc#object |

[When(@"When I call the URL with the RequestObject and Type (.*)(.*)(.*)")]
public void WhenIBlablablabla<T>(string url, string request){}

&lt;T&gt; 是传入的类型,还是类似的东西?我想在dry的良好实践中尽可能概括我的specflow。

【问题讨论】:

    标签: c# asp.net-web-api cucumber specflow


    【解决方案1】:

    您可以用最少的代码重复做到这一点:

    public void WhenIBlablablabla(string url, string request, Type type)
    {
        // Do stuff
    }
    
    [When(@"When I call the URL with the RequestObject and Type (.*)(.*)(.*)")]
    public void WhenICallTheURLWithTheRequestObjectAndType(string url, string request, string type)
    {
        Type systemType = Type.GetType(type);
    
        WhenIBlablablabla(url, request, systemType);
    }
    

    请注意,此解决方案不允许您将泛型作为类型参数传递。您可以使用类反射来检查当前类的方法,以查看它们是否接受基于systemType 变量的参数和泛型类型。

    【讨论】:

      【解决方案2】:

      不能在步骤中使用泛型。 SpecFlow 将特征中的 step 参数转换为 step 绑定中的参数时,使用的是方法参数的类型信息。 所以步骤绑定方法定义的是你得到的类型,而不是特征文件中的内容。

      你看过 Step Argument Conversion 吗? 见http://www.specflow.org/documentation/Step-Argument-Conversions/

      SpecFlow 有默认的(它们是用于 int、float 等的那些),您可以添加自己的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-26
        • 1970-01-01
        相关资源
        最近更新 更多