我已经注意到了(非常野蛮地)使用cucumber-JVM,当你定义哪些步骤具有看似相同名称的特征时,它们被认为是可互换的.
特定
Feature: Grab that cash with both hands and make a stash
Scenario: Spend it
Given Jack has 5 dollars
When Jack wants to buy Lear Jet
Then He doesn't have enough cash
Scenario: aquire it
Given Jack is broke
But his wealth is slowly growing
When Jack has 5 dollars
Then He can afford a pack of gum
Scenario: own it
Given Jack is broke
But he has a job that's paid 5 dollar an hour
When He works an hour
Then Jack has 5 dollars
@Given("^Jack has 5 dollars$")
public void set_it() throws Throwable {
this.jack = new Person();
jack.setCashAmount(5);
}
@When("^Jack has 5 dollars$")
public void wait_it() throws Throwable {
jack.waitFor(wealth(is(equalTo(5))));
}
@Then("^Jack has 5 dollars$")
public void check_it() throws Throwable {
assertThat("Jack should have 5 dollars by now",
jack.getCashAmount(),
is(equalTo(5)));
}
什么时候
我用黄瓜运行这个功能(用mvn / intelliJ插件/ eclipse插件/魔法水晶球/其他荒谬的媒介)
然后
我收到以下消息:
cucumber.runtime.DuplicateStepDefinitionException: Duplicate step definitions in my.project.MySteps.wait_it() in file:[...] and my.project.MySteps.set_it() in file:[...]
at cucumber.runtime.RuntimeGlue.addStepDefinition(RuntimeGlue.java:33)
at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:151)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:68)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:41)
at cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:86)
...
还有一堆其他不相干的线,我会饶你的.
诊断
黄瓜似乎没有区别@Given(“a”),@ When(“a”)和@Then(“a”)(和@And和@But,我真的不知道它们的意图首先,因为那些只是语法糖“与我刚刚做的相同”)
但正如实施所暗示的那样,这三个步骤彼此截然不同.每个句子都正确地描述了测试应该做什么,并且随后的实现是单一的.
>“给定”步骤应设置测试前置条件
>“何时”步骤应触发某个动作或等待一个动作
>“然后”步骤应该在之后断言系统的状态.
我在那里错过了什么?
有没有办法告诉Cucumber如何不仅依赖于正则表达式而且依赖于步骤类型(对我来说,这应该是默认行为)?