2012-12-07 79 views
9

目前更PARAMS我们有这一步:specflow: - “找到的一步暧昧的步骤定义”时,1级比其他

[When(@"I set the scenario price for product (.*) to (.*)")] 
public void WhenISetTheScenarioPriceForProductPToN(string product, string priceStr) 

我要补充步骤:

[When(@"I set the scenario price for product (.*) to (.*) in the (.*) zone") 
public void WhenISetTheScenarioPriceInZoneForProductPToN(string product, string priceStr, string zone) 

然而规格流给出两个步骤之间的误差的“找到的步骤歧义步骤定义”。

我已经厌倦了:

[When(@"I set the scenario price for product (.*) to (.*) in the (.*) zone")] 
    [When(@"I set the scenario price for product (.*) to (.*)")] 
    public void WhenISetTheScenarioPriceInZoneForProductPToN(string product, string priceStr, string zone) 

但失败,出现“绑定错误:参数数量不匹配”我希望它会在第二个“什么时候”通过。

+0

我不能用测试中的类似步骤重现模糊步骤问题,但参数计数不匹配是因为每个正则表达式模式代码都会为方法生成一个参数。它似乎也不支持默认参数值。 –

+0

@Admin,这听起来像一个specflow中的bug,因为我不明白为什么“when”和“given”以不同的方式进行匹配。 –

回答

11

的问题是第二个你(。*)。这可以扩展为匹配“abc区域中的xyz”。你能改为匹配一个单词吗?即(\ w +)

[When(@"I set the scenario price for product (.*) to (\w+)")] 

那样会阻止abc区域中较短的Regex匹配。

您可以通过注释掉你不再当属性,方法和调试,看看比赛中较小的一个进行测试。

而且,你总是必须有相同数量的正则表达式作为参数,这就是为什么合并一个不工作。


这对我不起作用,这是由于“0.99”和“。”的价格。不与\ w匹配。然而,这是有效的:

[When(@"I set the scenario price for product (.*) to (\S+)")] 
public void WhenISetTheScenarioPriceForProductPToN(string product, string priceStr) 

因为我们的“测试值”没有空格。

+0

如果您正在使用数字,请考虑(\ d +(\。\ d +)?),它与小数位和无小数位匹配 – AlSki

3

我最初认为这是因为在第一步定义的结尾正则表达式:

[When(@"I set the scenario price for product (.*) to (.*)")] 

这将捕捉(或匹配),将进入你的后续定义相同的字符串。

但是,这两个步骤的实现方法包含模棱两可的参数类型。我不能开始复制这一点,因为我以前int(按我的意见),但使用string可以重现这个问题,因为string是模糊的。在步骤定义文件中作为参数提供的任何参数都可以视为string

更改您的步法以下几点:

public void WhenISetTheScenarioPriceInZoneForProductPToN(string product, double price, string zone) 

public void WhenISetTheScenarioPriceForProductPToN(string product, double price) 

现在虽然正则表达式并没有改变,因此在理论上仍然会贪婪地匹配,SpecFlow提供转换为原始类型(以及其他类型的通过自定义转换),以便忽略了其余的句子。这意味着,因为它检测到double后的最终string参数(相对于不能够决定判决的部分是否字符串参数匹配或具有嵌入多个参数),它一点也不含糊。