During the first two recipes we learnt the concept of BDD and the basics of Cucumber; now we know that we can benefit from BDD using Cucumber, so it is time to write the first Hello World Cucumber feature.
In the first recipe we've already successfully installed Ruby, RubyGems, bundle, and Rails. To write our first Cucumber feature, we need a Rails application with Cucumber installed.
Now we create a Rails project and install Cucumber in the project. Follow the given steps:
Create a new Rails app,
cucumber_bdd_how_to
, by running the following Rails command in the terminal:$ rails new cucumber_bdd_how_to
Add
gem 'cucumber-rails'
into the project's Gemfile; it should be similar to the following code snippet:source 'https://rubygems.org' gem 'rails', '3.2.9' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' gem 'sqlite3' # Gems used only for assets and not required# in production environments by default.group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1' # See https://github.com/sstephenson/execjs#readme for # more supported runtimes# gem 'therubyracer', :platforms => :ruby gem 'uglifier', '>= 1.0.3' end gem 'jquery-rails' group :test do gem 'cucumber-rails' end # To use ActiveModelhas_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' # To use Jbuilder templates for JSON # gem 'jbuilder' # Use unicorn as the app server # gem 'unicorn' # Deploy with Capistrano # gem 'capistrano' # To use debugger # gem 'debugger'
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Run the bundle install in the terminal:
$ bundler install
After the installation is completed, cd into your RoR project directory and run:
$ rails generate cucumber:install
By running this, Cucumber will initialize a folder called
features
in your Rails project:Now we create a file under the
features
folder calledhello_world.feature
, and write down our first Cucumber test:Feature: Learn Cucumber As a Software Developer I want to learn Cucumber So that I can developer in BDD style! Scenario: Write Hello World Cucumber Given I have a Rails project When I write a Hello World Cucumber test Then I should be able to run it and see "Hello World" printed on screen
And we go to the terminal and run the following Cucumber command:
$ bundle exec cucumber features/hello_world.feature
Now we should see that it fails since we haven't implemented the steps yet. The message should be similar to the following screenshot:
Create a
hello_world_steps.rb
under thestep_definitions
directory.Copy the code shown on the console and paste it to
hello_world_steps.rb
.Modify the step code as follows:
Given /^I have a Rails project$/ do puts "Yes, I am at my RoR project." end When /^I write a Hello World Cucumber test$/ do puts "Yeah! I just wrote my test" end Then /^I should be able to run it and see "(.*?)" printed on screen$/ do |arg| puts arg end
Now we run it again and we see it successfully passed:
In this simple example, we wrote our first Cucumber feature named "Hello World", it has one scenario, "Write Hello World Cucumber", with three steps. We also implemented three step definitions and successfully made it pass.
In the Cucumber feature, one step is usually started with a preposition or an adverb (Given, When, Then, And, and But), each step is parsed and corresponding to a step definition, in our previous example the last step accepts one argument to be passed in, which means you can put any word in the step, and we passed the string Hello World
, so that it is printed on the screen.