Set up a testing framework for a Rails application.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
.gitignore
.ruby-gemset
.ruby-version
CHANGELOG.textile
Gemfile
LICENSE.txt
README.textile
Rakefile
rails_apps_testing.gemspec

README.textile

RailsApps Testing Gem RailsApps Testing Gem

See a blog post about Why I wrote this gem. I primarily use RSpec and Capybara for testing, but in principle, the gem could also set up Test::Unit or Cucumber frameworks. Also, you may want other configurations for RSpec, so let me know and we can extend the gem.

If you’re new to testing, I’ve written a tutorial on Rails testing with RSPec:

About

Use this gem to set up a testing framework. The gem modifies a Rails application and configures:

This suite of gems is popular for testing Rails applications. Typically, a developer makes several small configuration changes when setting up a test framework with these gems. The configuration changes are easy to make manually, but this gem provides a generator to make the changes, for ease of use with an automated process such as an application template.

The gem also offers options to install test suites for Devise or OmniAuth.

RailsApps Testing is a utility gem to use during development. You can remove it after setting up your test framework. It was originally written for use by the Rails Composer tool. Use Rails Composer to build any of the RailApps example applications for use as starter apps.

If you like the RailsApps Testing gem, you might be interested in the RailsLayout gem which generates Rails application layout files for various front-end frameworks such as Bootstrap and Foundation.

Join RailsApps

Support the RailsApps Project

If the RailsApps Testing gem is useful to you, please accept our invitation to join the RailsApps project. The RailsApps project provides example applications, tutorials, and starter tools, including the RailsApps Testing gem.

Install a Testing Framework

The RailsApps project offers a tutorial on Rails testing with RSPec:

To install a testing framework, add the gems you need. Then use the RailsApps Testing gem. It will set up and configure your testing framework.

Add the gems you need to your Rails application Gemfile:

group :development do
  gem 'rails_apps_testing'
end
group :development, :test do
  gem 'rspec-rails'
  gem 'factory_bot_rails'
end
group :test do
  gem 'capybara'
  gem 'database_cleaner'
  gem 'launchy'
  gem 'selenium-webdriver'
end

You don’t need the RailsApps Testing gem deployed to production, so put it in the development group.

If you want to use a newer unreleased version from GitHub:

group :development do
  gem 'rails_apps_testing', github: 'RailsApps/rails_apps_testing'
end

Use Bundler

Use Bundler to install the gems:

$ bundle install

Configure RSpec and Friends

To run the generator and configure the testing framework:

$ rails generate testing:configure rspec

Use --force if you want to overwrite existing files:

$ rails generate testing:configure rspec --force

RSpec Configuration

The RailsApps Testing generator will remove the legacy test/ folder if it exists (it is not needed for RSpec).

The RailsApps Testing generator will run rails generate rspec:install to create three files and a folder:

  • .rspec
  • spec/
  • spec/spec_helper.rb
  • spec/rails_helper.rb

Then the RailsApps Testing generator will configure RSpec.

It will modify the file .rspec to add:

--format documentation
--require rails_helper

and will remove:

--warnings

Prior to RSpec 3.0, every RSpec test file had to include require 'spec_helper'. With RSpec 3.0 and later versions, an additional require 'rails_helper' became necessary. Fortunately, starting with RSpec 3.0, to eliminate clutter and duplication, these requirements can be specified in the project setting .rspec file.

It will modify the file config/application.rb to suppress creation of stub files that many developers don’t use:

config.generators do |g|
  g.test_framework :rspec,
    fixtures: true,
    view_specs: false,
    helper_specs: false,
    routing_specs: false,
    controller_specs: false,
    request_specs: false
  g.fixture_replacement :factory_bot, dir: "spec/factories"
end

Capybara and Launchy Configuration

The generator will set up Capybara and Launchy to display CSS and JavaScript with a file spec/support/capybara.rb:

Capybara.asset_host = 'http://localhost:3000'

Database Cleaner Configuration

The generator will set up Database Cleaner to test JavaScript with a file spec/support/database_cleaner.rb:

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end
end

It will also modify spec/rails_helper.rb:

config.use_transactional_fixtures = false

FactoryBot Configuration

The generator will set up FactoryBot shortcuts with a file spec/support/factory_bot.rb:

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

Install Devise Tests

If you want to install tests for Devise, you can run:

$ rails generate testing:configure devise

Install OmniAuth Tests

If you want to install tests for OmniAuth, you can run:

$ rails generate testing:configure omniauth

Issues

Any issues? Please create an issue on GitHub. Reporting issues (and patching!) helps everyone.

Credits

Daniel Kehoe maintains this gem as part of the RailsApps project.

Please see the CHANGELOG for a list of contributors.

Is the gem useful to you? Follow the project on Twitter: @rails_apps. I’d love to know you were helped out by the gem.

MIT License

MIT License

Copyright © 2014 Daniel Kehoe

Useful Links

Getting Started Articles Tutorials
Ruby on Rails Analytics for Rails Rails Bootstrap
What is Ruby on Rails? Heroku and Rails Rails Foundation
Learn Ruby on Rails JavaScript and Rails RSpec Tutorial
Rails Tutorial Rails Environment Variables Rails Devise Tutorial
Ruby on Rails Tutorial for Beginners Git and GitHub with Rails Devise RSpec
Install Ruby on Rails Send Email with Rails Devise Bootstrap
Install Ruby on Rails – Mac OS X Haml and Rails Rails Membership Site with Stripe
Install Ruby on Rails – Ubuntu Rails Application Layout Rails Subscription Site with Recurly
Ruby on Rails – Nitrous.io HTML5 Boilerplate for Rails Startup Prelaunch Signup Application
Update Rails Example Gemfiles for Rails
Rails Composer Rails Application Templates
Rails Examples Rails Product Planning
Rails Starter Apps Rails Project Management