【问题标题】:How can I remove superfluous parameters from SpecFlow step method when using step argument transformation?使用步骤参数转换时,如何从 SpecFlow 步骤方法中删除多余的参数?
【发布时间】:2017-12-13 03:34:49
【问题描述】:

我将 SpecFlow 步骤转换为依赖于 StepArgumentTransformation,因此我不必处理多个原始参数,而是可以使用由转换方法构建的单个复杂参数。

但我了解到我无法从步骤中删除原始参数,因为这会导致“参数计数不匹配”异常。

[Given("(something (\d) (\d))")]
public void Step(Something something)
{
    // Does not work (parameter count mismatch)
}

[Given("(something (\d) (\d))")]
public void Step(Something something, int x, int y)
{
    // Works, but is slightly confusing due to unused parameters
}

[StepArgumentTransformation("something (\d) (\d)")]
public Something Transform(int x, int y)
{
    var something = new Something(x, y);
    return something;
}

如何去掉step方法中的xy参数?

【问题讨论】:

    标签: c# specflow


    【解决方案1】:

    您必须调整绑定的正则表达式。

    这是一个具体的例子:

    [Given("the coordinates are '(.*)'")]
    public void Step(Something something)
    {
        // Does not work (parameter count mismatch)
    }
    
    [StepArgumentTransformation("X:(\d)/Y:(\d)")]
    public Something Transform(int x, int y)
    {
        var something = new Something(x, y);
        return something;
    }
    
    Usage:
    
    Scenario: coordinate system
    
        Given the coordinates are 'X:1/Y:2'
    

    你可以在这里找到一个例子:https://github.com/techtalk/SpecFlow-Examples/tree/master/SpecFlow.Examples.StepArgumentTransformation

    Step Argument Transformation 的文档在这里:http://specflow.org/documentation/Step-Argument-Conversions/

    【讨论】:

    • 酷,谢谢!实际上,SpecFlow 使用了括号,并且玩弄它们似乎已经成功了:(something \d \d) 被解释为映射到单个参数(请注意只有一对括号),并且 SpecFlow 使用转换来提供该值论据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多