我有以下两个步骤定义:
[When(@"I Schedule a Start Now Event named ""(.*)"" for the DR program named ""(.*)""")]
[When(@"I Schedule a Start Now Event named ""(.*)"" for the DR program named ""(.*)"" with load control device ""(.*)""")]
在场景中使用上述两个步骤定义时,我可以看到第二步定义无法绑定。
这是因为,当我注释掉第二步定义时,我的步骤将按以下方式对第一步定义进行投标:
When I Schedule a Start Now Event named "~@+Event" for the DR program named "~@=Program" with load control device "Light"
术语"~@=Program" with load control device "Light"
被假定为单个字符串值。但它不应该是。
它应该与第二步定义绑定。
这是一个正则表达式模式问题吗?
答案 0 :(得分:1)
它是一个正则表达式模式问题,但也是一个相当普遍的问题。
基本上正则表达式.*
被称为贪婪。它会匹配它出现的所有内容,直到某些东西强迫它不匹配。艾伦摩尔建议使用引号将非常有效。另一个是使用不包含空格的名称,然后你可以使用[^ ]*
意思匹配许多不是空格。
我实际上建议您可以更轻松地使用较小的语句构建查询。例如
Given a DR program called xxx
And a load control device xxx
When I schedule a Start now event named xxx
private string drProgramName;
private string loadControlDevice;
[Given("a DR program called (.*)"]
public void GivenADRProgramCalled(string name)
{
drProgamName = name;
}
[Given("a load control device (.*)"]
public void GivenALoadControlDevice(string name)
{
loadControlDevice = name;
}
[When("I schedule a start now event named (.*)")]
public void WhenIScheduleAStartNowEventNamed(string name)
{
WhenIScheduleAStartNowEventNamed(name, drProgamName, loadControlDevice);
}
答案 1 :(得分:0)
在我看来,你需要更加具体:
[When(@"I Schedule a Start Now Event named ""([^""]*)"" for the DR program named ""([^""]*)""")]
[When(@"I Schedule a Start Now Event named ""([^""]*)"" for the DR program named ""([^""]*)"" with load control device ""([^""]*)""")]
让*
不情愿也可能会这样做:
[When(@"I Schedule a Start Now Event named ""(.*?)"" for the DR program named ""(.*?)""")]
[When(@"I Schedule a Start Now Event named ""(.*?)"" for the DR program named ""(.*?)"" with load control device ""(.*?)""")]
...但是否定的角色类更加万无一失。免责声明:我自己不是SpecFlow用户,但this blog post似乎证实了我的猜测。
答案 2 :(得分:0)
这篇Blog帖真的解决了我的问题。干杯艾伦!!