Subscribe :: Bookmark and Share

Random ravings from

sponsors

eBay Auctions

HP PROLANT DL360 SERVER Xeon 3.06 GHZ 3GB RAM 2x72.8 GB   HP PROLANT DL360 SERVER Xeon 3.06 GHZ 3GB RAM 2x72.8 GB
USD$244.00 Bids: 0
Location: US
NEW Sun Fire V245 2x 1.5-GHz UltraSPARC IIIi 16G Server   NEW Sun Fire V245 2x 1.5-GHz UltraSPARC IIIi 16G Server
USD$4,795.00 Bids: 0
Location: US
HP PROLANT DL360 SERVER Xeon 3.06 GHZ 1024 MB 2x72.8 GB   HP PROLANT DL360 SERVER Xeon 3.06 GHZ 1024 MB 2x72.8 GB
USD$324.00 Bids: 0
Location: US
Intel Xeon 3.4GHz 2MB 800 FSB SL7ZD Server CPU 3.4 GHz   Intel Xeon 3.4GHz 2MB 800 FSB SL7ZD Server CPU 3.4 GHz
USD$80.00 Bids: 0
Location: US
HP PROLANT DL360 SERVER Xeon 3.06 GHZ 1024 MB 2x72.8 GB   HP PROLANT DL360 SERVER Xeon 3.06 GHZ 1024 MB 2x72.8 GB
USD$399.00 Bids: 0
Location: US
IBM SERVER  PROCESSOR 3.06 GHZ FRU-71P8340 XEON SL6VP   IBM SERVER PROCESSOR 3.06 GHZ FRU-71P8340 XEON SL6VP
USD$159.00 Bids: 0
Location: US
HP PROLANT DL360 SERVER Xeon 3.20 GHZ 1024 MB 2x72.8 GB   HP PROLANT DL360 SERVER Xeon 3.20 GHZ 1024 MB 2x72.8 GB
USD$274.00 Bids: 0
Location: US
HP PROLIANT DL585 SERVER!!! DUAL 2.6 GHZ OPTERON!!!   HP PROLIANT DL585 SERVER!!! DUAL 2.6 GHZ OPTERON!!!
USD$999.00 Bids: 0
Location: US
Dell PowerEdge 2550 Server  w/SL5QJ P3 1.0 Ghz CPU   Dell PowerEdge 2550 Server w/SL5QJ P3 1.0 Ghz CPU
USD$159.00 Bids: 0
Location: US
INTEL QUAD CORE XEON 5330 2.1 GHZ SERVER CPU ACTIVE FAN   INTEL QUAD CORE XEON 5330 2.1 GHZ SERVER CPU ACTIVE FAN
USD$189.00 Bids: 0
Location: US

14 October 2008

Getting to know Cucumber with Rails

Comments (0)

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.

Leave a comment
Name (required)
 
Mail (will not be published)(required)
 
Website
simple_captcha.jpg
(type the code from the image)
Are you human?
 

 

Back