Thursday, 10 October 2013

Cucumber and Ruby, Introduction

I was using RSpec, as my testing framework , as part of my daily Rails development since couple of years.  Recently I happened to try Cucumber. In that process I decided to use Cucumber Ruby than Rails.

Cucumber ?
cucumber image
 Lets us look at the exiting business practice.

Who owns the business ?
Some one who have an idea.

Then why do we need software developers or “engineers” ?

Why can not business people do development as well ? They can express their thoughts in some language, English, Hindi, Spanish etc ...

Instruction to computer:
“Let there be a Robot that can move in one direction”

magic cap

Nothing happens .. Glad that  our computers does not produce software based on instructions in spoken languages, yet. (fortunately !).

As long as this is the case, we the software engineers are safe :)

What to expect from Cucumber

  1. Cucumber lets software development teams describe how software should behave in plain text, spoken languages. 
  2. Reduces the gap between business requirement and developed product( Agile to the rescue). 
  3. Develop only what is required
  4. Documentation
  5. Acceptance test. Some defined way of proving that developer met the expectation.

TDD - In general
Cucumber and TDD

Cucumber and TDD diagram
I am not sure if business people would get involved as much I expected here. 

Cucumber steps

cucumber steps - diagram

How does  a feature file look like ?

Feature: Serve coffee
  In order to earn money
  Customers should be able to
  buy coffee at all times

  Scenario: Buy last coffee
    Given there are 1 coffees left in the machine
    And I have deposited 1$
    When I press the coffee button
    Then I should be served a coffee
  Scenario: Buy first coffee
    Given there are 10 cofee left
    When I press the coffee button
    Then I should be served a coffee

    And number of coffee became 9

  • Notice indentation.
  • A line starting with the keyword Feature followed by free indented text starts a feature. A feature usually contains a list of scenarios. You can write whatever you want up until the first scenario, which starts with Scenario.
  • Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And.

Cucumber supports over 40 spoken languages and the number is steadily growing.

A # language: header on the first line of a feature file tells Cucumber what spoken language to use – for example # language: fr for French. If you omit this header, Cucumber will default to English (en).

feature file snapshot



Cucumber lets software development teams describe how software should behave in plain text.
The text is written in a business-readable domain-specific language -
The language that Cucumber understands is called Gherkin -
Gherkin is the language that Cucumber understands.
It is a Business Readable, Domain Specific Language that lets you describe software’s behavior
 without detailing how that behavior is implemented.()

Gherkin’s grammar is defined in the Treetop grammar
    Single Gherkin source file contains a description of a single feature.
Source files have .feature extension.
Like Python and YAML, Gherkin is a line-oriented language that uses indentation to define structure.
Line endings terminate statements (eg, steps).

Either spaces or tabs may be used for indentation (but spaces are more portable). Most lines start with a keyword.


Moving robot, but only in one direction.

Accept 2 params, step and direction

Ex: 'F', 10 OR 'B', 20

Moves either forward or back ward, Initially at the center (0).

Installing cucumber
$ gem install cucumber
Building native extensions.  This could take a while...
Successfully installed builder-3.2.2
Successfully installed diff-lcs-1.2.4
Successfully installed multi_json-1.8.0
Successfully installed gherkin-2.12.1
Successfully installed multi_test-0.0.2
Successfully installed cucumber-1.3.8
6 gems installed

No such file or directory - features. Please create a features directory to get started. (Errno::ENOENT)

Create directory and then create 'features/robot.feature'
Feature: Robot in Motion
  In order to move my robot
  As an owner
  I want the ability to create and move based on instructions
  Scenario: Create a robot
    Given Robot created
    When I check the position
    Then Robot should be at 0
  Scenario: Move forward
    Given Robot is at 0
    When I enter 'F', 10
    And I check the position
    Then Robot should be at 10

Run again and see it fails.

Now create 'features/step_definitions/robot_steps.rb' as

require 'robot'

Given(/^Robot created$/) do
  @robot =
  # pending # express the regexp above with the code you wish you had

When(/^I check the position$/) do
  @position = @robot.position
  # pending # express the regexp above with the code you wish you had

Then(/^Robot should be at (\d+)$/) do |arg1|
  @position.should == arg1.to_i
  # pending # express the regexp above with the code you wish you had

Given(/^Robot is at (\d+)$/) do |arg1|
  @robot =
  # pending # express the regexp above with the code you wish you had

When(/^I enter 'F', (\d+)$/) do |arg1|
        @robot.move('F', arg1.to_i)
  # pending # express the regexp above with the code you wish you had

6. This will again fail, create the actual robot class now - $ robot.rb

class Robot
        attr_accessor :location

        def initialize
                @location = 0

        def move(dir, step)
                if dir=='F'
                        @location = @location + step
                elsif dir=='B'
                        @location = @location - step

        def position

 7. $cucumber -f html > report.html 

This gives you some level of documentation, something like ...

cucumber report


Wednesday, 4 September 2013

Installing and configuring Ruby and Rails in Ubuntu 13.04 . Addressing issues with libxml2, nokogiri

After upgrading Ubuntu to 13.04(Raring Ringtail), I started having problems with bundle install and starting  server, main concern was libxml2. 

I managed to solve the issue using 'rvm pkg' and 'build.config'. Finally I thought of writing those steps used. The entire steps of installing rvm, ruby, libraries with some notes about the issues with lixml2.

Libraries required:

Assuming that user has an UBUNTU machine.

Install below given libraries. ex: $sudo apt-get install curl. Multiple libraries can be installed separated by spaces. You can skip those libraries that are not required. Below given list has most of the common libraries required.

$sudo apt-get install curl build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison
mysql-server mysql-client libmysqlclient-dev libxml2-dev libxslt-dev imagemagick

$ sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev(required for curb)

$ sudo apt-get install graphicsmagick-libmagick-dev-compat (required for RMagick,  libmagick9-dev doesn not exists any more)

$ sudo apt-get install libmagickcore-dev libmagickwand-dev (If you get ' wand/MAgickWand.h missing' error)

 Installing RVM(

 $ \curl -L | bash -s stable

Folow instructions if any. I had instructions to add "source ~./profile" in to  '~/home/shilesh/.bash_profile'

# making sure that it is loaded for the current instance also
            $source ~/.profile

Close out your current shell or terminal session and open a new one (preferred). You may load RVM with the following command:

user$ source ~/.rvm/scripts/rvm
If installation and configuration were successful, RVM should now load whenever you open a new shell. This can be tested by executing the following command which should output 'rvm is a function' as shown below.

user$ type rvm | head -n 1
rvm is a function

Installing  ruby

$ rvm list known   #This will show the available ruby options.

$ rvm install 1.8.7 # This will install Ruby 1.8.7

$ rvm use 1.8.7

$ rvm --default 1.8.7 # makes 1.8.7 as default ruby version.

$ ruby -v  # should give you something like ruby 1.8.7

Source  code from Git or ?.

You are expected to have ssh keys in .ssh folder of your profile (ex: /home/shilesh/.ssh/id_rsa. And it should have 066 permissions).
$ git clone<project>/<name>.git
# using https will promt for user name and password for each pull and push.
$ git checkout <some_branch> # move to development branch, if not master.

# Edit the values in database.yml with your local mysql server details. If you do not have server ready, update yml file with a user name and password which you will give to the server.

Installing Gems using bundler

Install ruby gems from "" and install.
$gem -v    # This might give you 2.0.6 or greater.

If you did not get the correct version to download, use
$ gem update --system 1.3.7 # This will update the gem to 1.3.7 version
 $ gem install bundler # Installing Bundler gem

We are ready to install all the gems, this might take some time based on your gems.
$bundle install --local


$ bundle exec rake db:create     # This will create database for development

$bundle exec rake db:migrate    # Creates schema

$bundle exec rake db:seed         # Load default values.


$ bundle exec script/server        # Your server should be up now on 3000 port.


Failed undefined method "source index for module"

reason could be  wrong version of gem.  
    $ gem update --system
    # gem install rubygems-update -v 1.3.7

There were cases where installing libxml-ruby fails as,

Installing libxml-ruby (1.1.3) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/home/shilesh/.rvm/rubies/ruby-1.8.7-p374/bin/ruby extconf.rb 
checking for socket() in -lsocket... no
checking for gethostbyname() in -lnsl... yes
checking for atan() in -lm... no
checking for atan() in -lm... yes
checking for inflate() in -lz... yes
checking for iconv_open() in -liconv... no
checking for libiconv_open() in -liconv... no
checking for libiconv_open() in -llibiconv... no
checking for iconv_open() in -llibiconv... no
checking for iconv_open() in -lc... yes
checking for xmlParseDoc() in -lxml2... yes
checking for libxml/xmlversion.h... no
checking for libxml/xmlversion.h in /home/shilesh/.rvm/rubies/ruby-1.8.7-p374/include,/home/shilesh/.rvm/rubies/ruby-1.8.7-p374/include/libxml2,/opt/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2... yes
creating extconf.h
creating Makefile

And then, even after bundler successfully installed all gems, server might fail as,

$ bundle exec script/server 
=> Booting WEBrick
=> Rails 2.3.17 application starting on
/home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/nokogiri-1.5.6/lib/nokogiri/ /usr/lib/i386-linux-gnu/ symbol xmlBufUse, version LIBXML2_2.9.0 not defined in file with link time reference - /home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/nokogiri-1.5.6/lib/nokogiri/ (LoadError)
from /home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/polyglot-0.3.3/lib/polyglot.rb:63:in `require'

In that case, try to specify the library after getting them with in rvm.

$ rvm pkg install libxml2 --verify-downloads 1

$ bundle config build.libxml-ruby --with-xml2-lib=${HOME}/.rvm/usr/lib --with-xml2-include=${HOME}/.rvm/usr/include/libxml2

$ bundle config build.nokogiri --with-xml2-lib=${HOME}/.rvm/usr/lib --with-xml2-include=${HOME}/.rvm/usr/include/libxml2

Then it is better to re-install all the gems using bundler. I deleted my gemset and created this again. After that installed bundler gem. Did the configurations as mentioned above, then ran bundler.

Everything was working fine then.

Friday, 3 May 2013

RMagick gem and UBUNTU 13.04

I recently upgraded my OS from Ubuntu 12.04 ro 13.04, Since then my 'rmagick' gem was not working. I used to get below error while starting my rails server ; cannot open shared object file: No such file or directory - /home/shilesh/.rvm/gems/ruby-1.8.7-p352@rails_2.3.16/gems/rmagick-2.12.2/lib/ (LoadError) 

Installing gem using bundler used to give below error;

$ gem install rmagickBuilding native extensions.  This could take a while...ERROR:  Error installing rmagick: ERROR: Failed to build gem native extension.
        /home/shilesh/.rvm/rubies/ruby-1.8.7-p352/bin/ruby extconf.rbchecking for Ruby version >= 1.8.5... yeschecking for gcc... yeschecking for Magick-config... yeschecking for ImageMagick version >= 6.4.9... yeschecking for HDRI disabled version of ImageMagick... yeschecking for stdint.h... yeschecking for sys/types.h... yeschecking for wand/MagickWand.h... no
Can't install RMagick 2.13.2. Can't find MagickWand.h.*** extconf.rb failed ***Could not create Makefile due to some reason, probably lack ofnecessary libraries and/or headers.  Check the mkmf.log file for moredetails.  You may need configuration options

Finally I re-installed ImageMagick and RMagick gem as given below, that worked,finally.

$ sudo apt-get install imagemagick

$sudo apt-get install libmagickwand-dev

$ gem install rmagick
Building native extensions.  This could take a while...
Successfully installed rmagick-2.13.2
1 gem installed
Installing ri documentation for rmagick-2.13.2...
Installing RDoc documentation for rmagick-2.13.2..