This example will use Rake to run cucumber because the Cucumber::Rake::Task does a lot of work for you. It will automatically load (require) the features and steps for you.
First, you should follow Cucumber's pattern for directories:
{project home}/features | stores all the .feature files |
{project home}/features/step_definitions | stores the .rb files containing steps |
{project home}/features/support | contains the env.rb file |
First create a rakefile for your project and add a Cucumber::Rake::Task as follows...
Next create the features file(s). The rake task will automatically require all the .feature files in the features directory. This example reproduces the additions.feature shown on Cucumber's homepage.
At this time, you may run "rake features" to see Cucumber runs. The results will show that 4 steps are undefined. To define the steps create features\step_definitions\addition_steps.rb. The rake task will automatically require all the step_definitions files.
This example uses a class Calculator from my project. So lets define that class under lib\calculator.rb.
This calculator isn't very exciting. It just uses a stack to remember values. The first item in the stack is the "screen". The add method will add the top two values. This is the bare minimum needed to meet the defined feature. (Normally you would define the class and fill in the details in a test-first fashion. But to keep this article short, I've posted the completed class.)
The rake features task doesn't automatically require the calculator.rb file. Also, if you noticed the addition_steps.rb makes use of rspec expectations, which isn't available by default. To require these files, create the features/support/env.rb file. The rake features task will automatically require any file under features/support
Now when you run "rake features" everything will work together. Your steps should all run and pass with green color!