我刚开始接触 SpecFlow 的世界,并且遇到了第一个问题。
在保持代码干燥方面,我想执行以下操作:
有两种情况:
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 时,我不断收到异常?参数。
我已经检查了正则表达式,它确实按预期解析了场景。
我知道我可以做一些像方法重载等粗略的事情,我只是想知道 SpecFlow 是否支持默认参数值的想法,或者实际上是另一种实现相同效果的方法。
非常感谢
(尚)不支持默认值,但对于您的具体情况,我可以建议以下内容:
SpecFlow 支持创建“步骤参数转换”。使用它们,您可以创建可以从不同模式解析日期时间的方法:
[StepArgumentTransformation("todays date")]
public DateTime TransformToday()
{
return DateTime.Today;
}
[StepArgumentTransformation("todays date (plus|minus) (\d+) days")]
public DateTime TransformOtherDay(string direction, int days)
{
//...
}
之后,您只需在步骤中使用 DateTime 参数,其余的由 SpecFlow 完成...
[Given(@"myfield equals (.*)")]
public void MyfieldEqualsTodaysDate(DateTime date)
{
//do stuff
}
您可以在以下位置查看更多示例https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)