I am just learning Cucumber and notice that if two completely seperate features have two steps that are accidentally worded the same, Cucumber suggests only one step definition for them. Does this mean that step definitions are global and they are meant to be shared?
Suppose a team of business analysts is writing specs for a financial firm that has a banking division and a brokerage division. Further assume that two different people are writing features for their respective divisions to calculate transaction fees.
The banking guy writes:
Feature: Transaction Fees
Scenario: Cutomer withdraws cash from an out-of-netwrok ATM
Given that a customer has withdrawn cash from an out-of-netwrok ATM
When I calculate the transaction fees
Then I must include an out-of-netwrok ATM charge
The brokerage guy writes
Feature: Transaction Fees
Scenario: Cutomer places a limit order
Given that a customer has placed a limit order
When I calculate the transaction fees
Then I must include our standard limit-order charge
Note that the When clause is the same for both the scenarios. Even worse, both guys put this scenario in a file called transaction-fees.feature (in different directories of course).
Cucumber produces the following recommendation for step definitions:
You can implement step definitions for undefined steps with these snippets:
this.Given(/^that a customer has withdrawn cash from an out\-of\-netwrok ATM$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.When(/^I calculate the transaction fees$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Then(/^I must include an out\-of\-netwrok ATM charge$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Given(/^that a customer has placed a limit order$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Then(/^I must include our standard limit\-order charge$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
Note that the when clause is suggested only once.
Thanks in advance for your time and clarifications.
step defs are attached to the World object, which is "this" in your code above.
There should only be one step definition. They are meant to be shared. IIRC, the Cucumber Boo, page 149 (https://pragprog.com/book/hwcuc/the-cucumber-book) gets into the details of this design decision. Although it is ruby, I think this is the same across all cucumber implementations.
Cucumber does not associate feature files and step_definition files. The file tree/convention is for convenience only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With