默认值Specflow Step Definitions

Gav*_*orn 6 .net testing bdd specflow

我开始在SpecFlow的世界里,我遇到了我的第一个问题.在保持我的代码DRY方面,我想做以下事情:

有两种情况:

Given I am on a product page
And myfield equals todays date
Then...

Given I am on a product page
And myfield equals todays date plus 4 days
Then...

我希望使用以下步骤定义来涵盖我的And子句的两个变体:

[Given(@"myfield equals todays date(?: (plus|minus) (\d+) days)?")]
public void MyfieldEqualsTodaysDate(string direction, int? days)
{
//do stuff
}

但是,当SpecFlow尝试解析int时,我一直得到异常?PARAM.我检查了正则表达式,它肯定按预期解析了场景.我知道我可以像方法重载那样粗糙等等,我只是想知道SpecFlow是否支持默认参数值的想法,或者实际上是另一种方法来实现相同的效果.

非常感谢

Gas*_*agy 8

(尚未)支持默认值,但对于具体情况,我可以建议以下内容:

SpecFlow支持创建"步骤参数转换".使用它们,您可以创建可以从不同模式解析日期时间的方法:

[StepArgumentTransformation("todays date")]
public DateTime TransformToday()
{
  return DateTime.Today;
}
[StepArgumentTransformation("todays date (plus|minus) (\d+) days")]
public DateTime TransformOtherDay(string direction, int days)
{
  //...
}
Run Code Online (Sandbox Code Playgroud)

之后,您只需在步骤中使用DateTime参数,其余部分由SpecFlow完成...

[Given(@"myfield equals (.*)")]
public void MyfieldEqualsTodaysDate(DateTime date)
{
  //do stuff
}
Run Code Online (Sandbox Code Playgroud)

你可以在https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions看到更多的例子