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:
Use this gem to set up a testing framework. The gem modifies a Rails application and configures:
- RSpec Rails – installs RSpec gems with support for Rails
- Capybara – tests web pages
- Database Cleaner – a clean slate for databases
- FactoryBot Rails – creates test data
- Launchy – view errors in your web browser
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.
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
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 to install the gems:
$ bundle install
Configure RSpec and Friends
To run the generator and configure the testing framework:
$ rails generate testing:configure rspec
--force if you want to overwrite existing files:
$ rails generate testing:configure rspec --force
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:
Then the RailsApps Testing generator will configure RSpec.
It will modify the file .rspec to add:
--format documentation --require rails_helper
and will remove:
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
Capybara.asset_host = 'http://localhost:3000'
Database Cleaner Configuration
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
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
Any issues? Please create an issue on GitHub. Reporting issues (and patching!) helps everyone.
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.
Copyright © 2014 Daniel Kehoe