Testing Setup in Rails
A quick guide to setting up testing environment in Rails using RSpec, Capybara, FactoryGirl, and Konacha.
Gems
The gems gonna used are:
- rspec/rspec-rails: BDD Testing Framework for Rails.
- jnicklas/capybara: Acceptance Testing Framework.
- jfirebaugh/konacha: JavaScript Testing for Rails (Mocha + Chai).
- jonleighton/poltergeist: PhantomJS driver for Capybara.
- thoughtbot/factory_girl_rails: Fixtures.
- bmabey/database_cleaner: Database Cleaning.
Gemfile
Setup
- Initialize RSpec with
rails generate rspec:install
. This create thespec/
directory and addsspec/spec_helper.rb
- Configure Capybara by add
require 'capybara/rails'
tospec_helper.rb
. Note: Spec files should created under folderspec/features
. - Configure DatabaseCleaner by adding following lines to
spec_helper.rb
:
- Setup Konacha by creating
spec/javascripts
folder. Optionally, with aspec_helper.js.coffee
file. - To use Poltergeist, PhantomJS is required. Then:
Refer to Gist spec_helper.rb.
Commands
RSpec
bundle exec rspec
: run all specs.bundle exec rspec spec/models
: run all model specs.bundle exec rspec spec/models/todo_spec.rb
: run specs in a file.
RSpec provides generator rails generate rspec:model widget
, which will create a new spec file in spec/models/widget_spec.rb
.
Available generators: scaffold
, model
, controller
, helper
, view
, mailer
, observer
, integration
.
Konacha
bundle exec rake konacha:serve
: serve specs at http://localhost:3500.bundle exec rake konacha:run
: run specs in command line.bundle exec rake konacha:run SPEC=foo_spec
: run specs in a file.
Additions
Include Helpers
Common helper functions used in testing can created in spec/support
folder. The helper files then can be included in spec_helper.rb
.