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

时间:2013-06-24 13:18:55

标签: c# specflow

我遇到这种情况:

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

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

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

2 个答案:

答案 0 :(得分:5)

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

请花点时间考虑GivenWhenThen步骤试图实现的目标,我认为这是:

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

所以最多你可能会认为如果你的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
}

希望这有帮助。

<强>更新

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

答案 1 :(得分:1)

我使用&#39; StepDefinition&#39;这种情况的属性。虽然使用它肯定意味着你打破了BDD设计理念。

[StepDefinition(@"I select cell (.+)")]
public void WhenIClickOnExcelCellX(string cell)
{
    excelDriver.SelectCell(cell);
}
相关问题