默认值Specflow步骤定义 [英] Default Values Specflow Step Definitions

查看:141
本文介绍了默认值Specflow步骤定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从SpecFlow的世界开始,遇到了第一个问题. 为了使代码保持干燥,我想执行以下操作:

I'm starting out in the world of SpecFlow and I have come across my first problem. In terms of keeping my code DRY I'd like to do the following:

有两种情况:

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
Then...

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

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

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

I was hoping to use the following Step Definition to cover both variants of my And clause:

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

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

但是,当SpecFlow尝试解析int时,我总是收到异常消息吗?参数. 我已经检查了正则表达式,它肯定按预期解析了该方案. 我知道我可以像方法重载之类的粗略方法,我只是想知道SpecFlow是否支持默认参数值的想法,或者确实是实现相同效果的另一种方法.

However I keep getting exceptions when SpecFlow tries to parse the int? param. I've checked the regular expression and it definitely parses the scenario as expected. I'm aware that I could so something as crude as method overloading etc, I was just wondering if SpecFlow supported the idea of default parameter values, or indeed another way to achieve the same effect.

非常感谢

推荐答案

尚不支持默认值,但是对于您的具体情况,我可以提出以下建议:

The default values are not supported (yet), but for your concrete case, I can suggest the following:

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

SpecFlow supports creating "step argument transformations". With them, you can create methods that can parse datetime from different patterns:

[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完成...

After that, you just need to use a DateTime param in your steps and the rest is done by SpecFlow...

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

您可以在 https://github.com/techtalk/上看到更多示例SpecFlow/wiki/Step-Argument-Conversions

这篇关于默认值Specflow步骤定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆