Thinking its time to delve into Cucumber, I have to say found it a bit difficult to grasp what was going on – hence this post.
What is Cucumber? - "a tool that can execute feature documentation written in plain text". So for BDD [Behavioural Driven Development – where feature descriptions & tests are typically written before the code], Cucumber is intended to help non-programmer types such as business analysts, business specialists and the like write tests.
This post covers how to install and write your first, and very basic, Cucumber test.
Webrat is also installed for feature testing websites - be they rails, php, .net, java, or anything else.
Install Cucumber into your rails site
[Windows users can ignore the “sudo” bit]
>sudo gem install cucumber
>sudo gem install term-ansicolor treetop diff-lcs hpricot
Now, Go to your project directory & execute the following:
>ruby script/plugin install git://github.com/aslakhellesoy/cucumber.git
>ruby script/plugin install git://github.com/brynary/webrat.git
You will need to install rspec if not already [read rspec on rails documentation to complete install]
>ruby script/plugin install git://github.com/dchelimsky/rspec.git
>ruby script/plugin install git://github.com/dchelimsky/rspec-rails.git
Bootstrap cucumber
>ruby script/generate cucumber
This will create:
features/steps
features/steps/env.rb
features/steps/common_webrat.rb
lib/tasks/cucumber.rake
script/cucumber
You will write features descriptions in features/my_feature.feature
And the steps to test these fixtures in features/steps/my_feature_steps.rb
Have a quick look at features/steps/env.rb – you can see the options available & how the environment is setup
Have a quick look at features/steps/common_webrat.rb – you can see all the appropriate “When I...” statements.
Now lets run the rake task to see test the install & confirm that something happens.
>rake features
On doing so, I received this error:
DEPRECATION WARNING: Inflector is deprecated! Use ActiveSupport::Inflector instead. See http://www.rubyonrails.org/deprecation for details. (called from /home/matt/development/tickex/vendor/plugins/active_merchant/lib/active_merchant/billing/integrations.rb:11)
I've got an old version of Active Merchant installed.. so time to update
>ruby script/plugin install git://github.com/Shopify/active_merchant.git --force
If all has run successfully you should see coloured Cucumber messages – successful steps are now in green, pending steps in yellow & failures in red. The backtrace can be found next to the failed step, instead of at the end as it used to to be in rspec.
Now lets create our own basic test. We are going to test that a user can browse to the home page. So create a new file called features/home_page_links.feature & fill it like so..
Feature: Home page links
In order to browse my_site.com
A user
Should be able to browse to the home page
Scenario: Browse to the home page
Given I am on the home page
Save the file then from the command line in the applications root directory run
> rake features
You should see something similar to:
Feature: Home page links # features/home_page_links.feature
In order to browse tickex.com
A user
Should be able to browse to the home page
Scenario: Browse to the home page # features/home_page_links.feature:6
Given I am on the home page # features/home_page_links.feature:7
1 steps pending
You can use these snippets to implement pending steps:
Given /^I am on the home page$/ do
end
The “1 steps pending” indicates that you have a feature description, but no test steps. So lets add that.
Create a new file features/steps/home_page_links_steps.rb and place this content in the file.
Given /I am on the home page/ do
visits "/"
end
The Given.. block, corresponds to Scenario statement in the feature file. We are using Webrat to perform the browsing. Now run
>rake features
You should now see something like..
Feature: Home page links # features/home_page_links.feature
In order to browse tickex.com
A user
Should be able to browse to the home page
Scenario: Browse to the home page # features/home_page_links.feature:6
Given I am on the home page # features/steps/home_page_links_steps.rb:1
1 steps passed
with “1 steps passed” being green to indicate all is good.
This should be enough for you to get Cucumber up & running with your rails site and to start making sense of the Cucumber examples.

Trackback URL