IntelliJ 要求在不同的 Cucumber 步骤之间选择声明

IntelliJ asking to choose declaration between Cucumber steps that are not the same

我有两个步骤有一些相似之处,但措辞并不完全相同。但是,IntelliJ 一直要求我在它们之间进行选择,就好像它们是重复的一样,但不确定为什么?

@And("^I add \"([^\"]*)\" as the history|comment|details for the item$")
public void iAddAsTheHistoryForTheItem(String text) {
    addComment(text);
}


@Then("^I am able to add a history|comment|details to the item$")
public void iAmAbleToAddAHistoryToTheItem() {
    addHistory();
    assertFalse(isHistoryClean());
}

谢谢。

它们包含很多相同的单词,使它们完全不同,不会让您选择。

这是因为步骤定义中的 OR 符号 (|)。

在你的例子中,它匹配两个步骤定义,因为 'comment'

这就像你对 IDE 说的:寻找 'I am able to add a history' 或寻找 'comment' 或寻找 'details to the item'(@Then("^I am able向项目添加历史|评论|详细信息$"))。

对于步骤定义@And("^I add \"([^\"])\" as the history|comment|details for the item$") 你有 -> 看因为我添加 \"([^\"])\" 作为历史或查找 'comment' 或查找 'details for the item'

我在这里看到 2 种可能的解决方案:

1) 您可以尝试以这种方式重构步骤定义以避免当前情况:

@Then("^我可以向项目添加(历史|评论|详细信息)$")

@And("^我添加 \"([^\"]*)\" 作为项目的(历史|评论|详细信息)$")

2) 你可以在小黄瓜中使用参数,这样你就可以在那里提供任何值,它不会建议你两个步骤定义

例如:

然后我可以添加 "put your value here" 到项目

对应的步骤定义为

 @Then("^I am able to add a \"([^\"]*)\" to the item$")
public void iAmAbleToAddAToTheItem(String arg0) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

(第二步做类似修改)