2016-06-07 155 views
0

如果在黄瓜步骤的任一末端,在黄瓜步骤中定义可选参数很容易。怎么样,如果它在中间,如:匹配是否可以在黄瓜步骤中定义可选参数

@When("^(in)?active update message is received for (\\D+)(?: (\\d))? (\\D+) (\\d+)$") 
public void testStepDef(
     final String optionalInactive, 
     final String someWord, 
     final int optionalInt, 
     final String someOtherWord, 
     final int id) { 

例模式:

* inactive update message is received for word anotherword 1 // not ok 
* active update message is received for word 1 anotherword 7 // ok 
* inactive update message is received for word 7 anotherword 3 // ok 

从这些例子中,只有后者的2工作;第一个,没有定义optionalInt,失败。正则表达式匹配,但方法invokation失败:

cucumber.runtime.CucumberException: Failed to invoke cucumber.stepdefs.common.CommonStepDefs.testStepDef(String,String,int,String,int) in file:/step/file/location 

任何方法来解决此限制?

+2

我不知道这是否是问题,但是您正在使用原语作为参数。一个int不能为null。如果您尝试使用Integer类型,那该怎么办?无论如何,如果黄瓜不能胜任,我可能只是将更多的解析放到步骤中。 –

回答

0

正如你只是使用正则表达式的,你可以做任何正则表达式可以做的事情。然而,你在这里做的是让你的步骤定义变得难以理解。

一般来说,当你想保持你的步骤定义尽可能简单。最好有3个简单的步骤定义,而不是一个复杂的定义。

因此,解决这个问题的最好方法是使用尽可能少的参数和最简单的正则表达式来编写更简单更清晰的步骤定义。