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 4.2.5 |
| $ bin/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:
| source 'https://rubygems.org' |
| |
| |
| # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' |
| gem 'rails', '~> 5.0.0', '>= 5.0.0.1' |
| # Use sqlite3 as the database for Active Record |
| gem 'sqlite3' |
| # Use Puma as the app server |
| gem 'puma', '~> 3.0' |
| # Use SCSS for stylesheets |
| gem 'sass-rails', '~> 5.0' |
| # Use Uglifier as compressor for JavaScript assets |
| gem 'uglifier', '>= 1.3.0' |
| # Use CoffeeScript for .coffee assets and views |
| gem 'coffee-rails', '~> 4.2' |
| # See https://github.com/rails/execjs#readme for more supported runtimes |
| # gem 'therubyracer', platforms: :ruby |
| |
| # Use jquery as the JavaScript library |
| gem 'jquery-rails' |
| # Turbolinks makes navigating your web application faster. Read mo... |
| gem 'turbolinks', '~> 5' |
| # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder |
| gem 'jbuilder', '~> 2.5' |
| # Use Redis adapter to run Action Cable in production |
| # gem 'redis', '~> 3.0' |
| # Use ActiveModel has_secure_password |
| # gem 'bcrypt', '~> 3.1.7' |
| |
| # Use Capistrano for deployment |
| # gem 'capistrano-rails', group: :development |
| |
| group :development, :test do |
| # Call 'byebug' anywhere in the code to stop execution and get a de... |
| gem 'byebug', platform: :mri |
| end |
| |
| group :development do |
| # Access an IRB console on exception pages or by using <%= console ... |
| gem 'web-console' |
| gem 'listen', '~> 3.0.5' |
| end |
| |
| # Windows does not include zoneinfo files, so bundle the tzinfo-data gem |
| gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] |
| |
| group :test do |
| gem 'cucumber-rails', '1.4.5', require: false |
| gem 'cucumber', '3.0.0.pre.1' |
| gem 'rspec-rails', '3.5.2' |
| gem 'database_cleaner', '1.5.3' |
| 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 |
| $ bin/rails generate cucumber:install |
Let’s take a quick look at the files that the generator has given us:
Contains some profiles that you can use to run Cucumber with different sets of options.
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.
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.
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:
| 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.
3.145.202.27