SpecFlow步骤的布尔参数

Dea*_*nes 8 c# specflow

在SpecFlow中,我想检查步骤定义中是否存在字符串,此时我正在做一些笨重的事情,例如这个人为的例子:

[Given(@"Foo ( bar)?")]
public void GivenFoo(string bar)
{
    if (bar == " bar")
    {
        // do bar
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我想做这样的事情:

[Given(@"Foo ( bar)?")]
public void GivenFoo(bool bar)
{
    if (bar)
    {
        // do bar
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我不知道怎么样,这是可能的,如果可能的话怎么样?

Eri*_*rik 5

你绝对可以用一种StepArgumentTransformation方法来做那种事情。您仍然需要编写解析器逻辑,但您可以将其隔离到一个仅用于执行解析的方法中。

示例特征文件:

Feature: I win if I am Batman

Scenario: Happy
    Given I am the Batman
    Then I defeat the Joker

Scenario: Sad
    Given I am not the Batman
    Then I am defeated by the Joker
Run Code Online (Sandbox Code Playgroud)

Specflow 绑定 (C#):

[Binding]
public class IWinIfIAmBatmanFeature
{
    private bool iAmBatman;

    [StepArgumentTransformation(@"(am ?.*)")]
    public bool AmToBool(string value)
    {
        return value == "am";
    }

    [Given(@"I (.*) the Batman")]
    public void WhoAmI(bool amIBatman)
    {
        iAmBatman = amIBatman;
    }

    [StepArgumentTransformation(@"(defeat|am defeated by)")]
    public bool WinLoseToBool(string value)
    {
        return value == "defeat";
    }

    [Then(@"I (.*) the Joker")]
    public void SuccessCondition(bool value)
    {
        Assert.AreEqual(iAmBatman, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

关键因素是您的Given子句中的正则表达式匹配由 step 参数转换匹配。因此I (.*) the Batman,如果捕获与 StepArgumentTransformation 参数中的正则表达式匹配,就像它在属性中所做的那样,AmToBool那么这就是使用的转换。


Jak*_*cki 0

您是否在寻找:

[Given(@"I do something (times) times")]
public void GivenIDoSomething(int times)
{
}
Run Code Online (Sandbox Code Playgroud)

否则这似乎就足够了

[Given(@"I do something twice")]
public void GivenIDoSomethingTwice()
{
}
Run Code Online (Sandbox Code Playgroud)

编辑

if我认为您实际上想要分隔步骤,而不是在步骤中声明。