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:
cucumber_bdd_how_to
, by running the following Rails command in the terminal:$ rails new cucumber_bdd_how_to
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'
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.
$ bundler install
$ rails generate cucumber:install
features
in your Rails project:features
folder called hello_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
$ bundle exec cucumber features/hello_world.feature
hello_world_steps.rb
under the step_definitions
directory.hello_world_steps.rb
.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
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.
13.58.51.36