Feature files cannot inherit. You should not be trying to do any programming with feature files ever!
The way to do this is
- Name what your common background does.
- Implement a helper method using that name
- Include something in your feature that refers to that name
- As you need additional setup find new names, create new methods and have them use existing methods by calls
The naming part is important to features. The rest is programming and has no place in features.
Lets do the login example, assuming we already have the functionality to register users and login
Scenario: Users can see their previous orders
Given I am signed in
When I view my orders
Then I should see my orders
# steps
Given 'I am signed in' do
sign_in user: @i
end
# Helper methods
module SignInStepHelper
def sign_in(user:)
register(user)
...
end
end
World SignInStepHelper
Now you can apply this pattern to any level of complexity, because once you get to the module part of your stack you are in a programming language, and here you can program any level of complexity into a single method call.
The secret is pushing 'HOW' you do this down so its at a lower level than the step definitions. Combine this with naming and you can deal with any level of complexity.
For example lets deal with a registered customer doing a repeat order of their monthly stationery
Scenario: Repeat order of monthly stationery
Given I am registered
And I have a monthly stationery order
When I repeat my monthly order
Then I should see my order in the checkout
And I should see my previous orders
# some steps
Given 'I have a monthly stationery order' do
create_monthly_order(
products: stationery_order,
user: @i
)
end
# implementation
module OrdersStepHelper
def create_monthly_order(products:, user:)
...
end
def stationery_products
[
...
end
end
Now clearly there is a lot more code required to make this work, but because this is pushed down below the step definitions this is just straightforward programming. So you'll need methods like stationery_order
, add_stock
etc. However if you are doing BDD by the time you get to implementing this behaviour you will have most of the other stuff done and available to you.
When cuking I have a hierarchy in mind which is
features
step definitions
step helpers
application
The further down this stack I can push code that does stuff the better my experience will be.