使用通配符属性的Specflow步骤定义映射

Vaj*_*jda 1 c# specflow

我有这种情况:

[Given(@"I select cell (.+)")]
[When(@"I select cell (.+)")]
[Then(@"I select cell (.+)")]
public void WhenIClickOnExcelCellX(string cell)
{
     excelDriver.SelectCell(cell);
}
Run Code Online (Sandbox Code Playgroud)

是否有任何通配符属性可以匹配这三个关键字中的任何一个?我想写这样的东西,不要担心我是否提供了该属性的映射.

[Any(@"I select cell (.+)")]
public void WhenIClickOnExcelCellX(string cell)
{
    excelDriver.SelectCell(cell);
}
Run Code Online (Sandbox Code Playgroud)

AlS*_*Ski 5

实际上我很确定没有,这是设计的.

花点时间考虑一下Given,WhenThen步骤正在努力实现,我想是这样的:

  • 给定 - 是一个先决条件,所以你并不关心被测代码如何进入该状态,只是状态在那里我们可以运行测试.
  • 当 - 是执行一个改变状态的动作,所以我们可以测试它
  • 然后 - 检查某些东西是否已进入正确的状态

所以最多你可能会认为如果你When I select cell x是一个相当轻量级的实现,你可以(但不一定应该)重用它Given I select cell x.

然而,你Then I select cell x真的无效,相反它应该是Then cell x should be selected,即

using Should;
[Then(@"cell (.+) should be selected")] //Regex might need changing
public void ThenCellXShouldBeSelected(string cell)
{
     excelDriver.IsSelected(cell).ShouldBeTrue(); //Or whatever the call is
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

更新

查看https://github.com/techtalk/SpecFlow/blob/master/Runtime/Attributes.cs上的代码,可以看出有一个基类,StepDefinitionBaseAttribute但它是抽象的.