This article describes the RSpec setup process in both Ruby and Rails. After setting up in whichever environment you'll be developing in, we recommend you check out Writing Specs in RSpec, and then move on to RSpec Methods.
In Plain Ruby:
- Conventionally, you'll establish the following file structure:
└── application_root ├── Gemfile ├── Rakefile ├── lib │ └── app.rb └── spec └── app_spec.rb
- The .rspec folder can contain this setting to colorize the output in the traditional red/green style:
- The Gemfile also only need include a single line:
rspec specas your default rake task. That way, you can simply type
raketo run your test suite. Save this code to your Rakefile:
begin require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) task default: :spec rescue LoadError # rspec not available end
- In the app_spec.rb file, at the top of the document, make sure to include:
require "rspec" require_relative "../lib/app"
Where app is the name of the app.rb file. Now your spec file is linked to your app file, and ready to rock and roll. Make sure, if you haven't already got the gem in the current gemset that you run:
To download RSpec and get moving.
- When generating a new Rails project, add the flag
--skip-test-unitto supress creation of the test directory associated with the default Test::Unit framework.
- In the Gemfile, add:
group :development, :test do gem 'rspec-rails' end
- The development mode RSpec files add RSpec-specific generators
- Test mode includes files to run the tests
- RSpec is a dependency of RSpec-Rails, so we don't need to include it.
- Run this snippet to configure Rails to use RSpec in place of Test::Unit
rails generate rspec:install
- Run bundle:
Retroactively Adding RSpec to a Rails App
-sto skip editing existing files
--migration=falseto skip creation of migration files ( note that '-s' should take care of this )
rails g model ModelName -s --migration=false
Updating the test database
- Check for pending migrations and load the test schema
raketo run your test suite.