使用步骤参数转换时,如何从SpecFlow步骤方法中删除多余的参数?

时间:2017-12-12 15:45:04

标签: c# specflow

我将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;
}

如何摆脱步骤方法中的xy参数?

1 个答案:

答案 0 :(得分:0)

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

这是一个具体的例子:

[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/