Running the Generators

Rails uses generators to help you set up your application with typical boilerplate code that you can then customize to your needs. First we’re going to generate the Rails application, and then we’ll add the cucumber-rails plug-in and use its generator to configure your application so it’s ready for you to start driving it with Cucumber.

We start by creating our Rails application. We’re going to use the rails new command with a single option:

 
$ ​gem install rails --version 3.1.3
 
$ ​rails new squeaker --skip-test-unit

The option doesn’t matter too much—those are parts of Rails that we’re not going to use, so they’d just be cluttering up our code if we left them in.

Now we need to edit our new Rails application’s Gemfile to include the cucumber-rails gem. We’re also including the rspec-rails gem, which we’ll use to make assertions later, and finally we’re adding database_cleaner, a gem that will make sure our database is cleaned up between each scenario:

rails/01/Gemfile
 
source ​'https://rubygems.org'
 
 
gem ​'rails'​, ​'3.2.7'
 
 
# 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'
 
 
# To use ActiveModel has_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'
 
 
group :test ​do
 
gem ​'cucumber-rails'​, ​'1.3.0'
 
gem ​'rspec-rails'​, ​'2.11.0'
 
gem ​'database_cleaner'​, ​'0.8.0'
 
end

We’ve tucked the Cucumber gems away in a test group so they won’t be installed when we deploy to production.

With that done, we can move our working directory into the new application’s folder and run the Cucumber generator:

 
$ ​cd squeaker
 
$ ​bundle install
 
$ ​rails generate cucumber:install

Let’s take a quick look at the files that the generator has given us:

config/cucumber.yml

Contains some profiles that you can use to run Cucumber with different sets of options.

features/support/env.rb

This is the script Cucumber runs first when the tests start. It loads the Rails environment and requires the various Cucumber and Capybara libraries needed to test your Rails application.

lib/tasks/cucumber.rake

Gives your application a cucumber Rake task. You can edit this file to change the default options for the Rake task or create new ones.

config/cucumber.yml

The generator adds a new cucumber environment that has the same configuration as the existing test environment.

Now that we have a Rails application with Cucumber set up, let’s put the feature into features/see_messages.feature:

rails/01/features/see_messages.feature
 
Feature:​ See Messages​
 
Scenario:​ See another user's messages​
 
Given ​there is a User​
 
And ​the User has posted the message ​"this is my message"​​
 
When ​I visit the page for the User​
 
Then ​I should see ​"this is my message"

Let’s see if we can put just a little flesh on the bones.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.136.63