在 Android 上使用 Cucumber 时出现 AmbiguousStepDefinitionsException

AmbiguousStepDefinitionsException while using Cucumber on Android

我正在使用日志获取 AmbiguousStepDefinitionsException:

cucumber.runtime.AmbiguousStepDefinitionsException: ✽.Given I am logged out(features/performance.feature:7) matches more than one step definition:
^I am logged out$ in PerformanceListSteps.i_am_logged_out()
^|I should be on |the list page$ in LoginSteps.i_should_be_on_the_list_page()

虽然运行黄瓜正在测试。我对这种态度有点陌生,我很乐意提供帮助。

问题在于第二个正则表达式模式:

^|I should be on |the list page$

竖线(|)符号是一个变体。所以这个正则表达式基本上意味着它将匹配其中一个备选方案:

  • 一个空字符串(第一个|的左边)
  • I should be on
  • the list page

匹配空字符串的正则表达式可以匹配 0 个字符,因此它可以匹配任何字符串。这在某种程度上取决于实现,但在 Cucumber-JVM 的情况下,我确认如果我将类似的正则表达式模式添加到我的步骤定义之一,它会匹配项目中的所有步骤。

解决方案

此处最好的解决方案是从正则表达式中删除管道,因为(通常)您希望正则表达式尽可能具体,只为进入步骤定义的变量留下变化。

所以你在这里需要的是将正则表达式模式更改为:

^I should be on the list page$

它将匹配任何 Gherkin 关键字(Given/When/Then 等)之后的语句的步骤,例如:

Then I should be on the list page