Setting Up RSpec in a Rails Project - marklocklear/railsgirls GitHub Wiki
Setup Your GEMFILE
Add the RSpec gems to the GEMFILE as a development and testing group:
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'shoulda-matchers'
end
Once you have edited your GEMFILE, use bundler to install the gems
bundle install
rspec-rails is the main RSpec gem that you use in a Rails project. Read more about RSpec here: http://rspec.info/
factory_girl_rails installs the FactoryGirl library. FactoryGirl creates "factories" that are used to generate useable test data for your specs. Read more about FactoryGirl here: http://github.com/thoughtbot/factory_girl
shoulda-matchers are a set of predefined specs that are very useful in the Rails ecosystem. Read more about Shoulda Matchers here: http://github.com/thoughtbot/shoulda-matchers
Install RSpec
Once you have all of the necessary Gems, you will run the RSpec install script:
script/rails generate rspec:install
This adds the spec directory and some skeleton files, including an .rspec file.
Add Flags to .rspec For Pretty and Verbose Output
At the root of your project directory is a file called .rspec. This file contains default flags that are included every time you run your specs. Don't worry too much about this right now. By adding these flags, you will get colorful and verbose output so that you can see what is happening with your specs.
Open .rspec and add the following flags:
--color
--format documentation
Setup Your Testing Database
In order to make your tests run fast and keep from corrupting your development or production databases, RSpec uses its own, independent database. This is much easier than it sounds. All you have to do is type:
rake db:test:prepare
You should do this every time you add or change a migration. This will keep you test database in sync with your development database.
Make Sure It Is Working
Now you want to try running your specs. If you want to run your entire test suite, simply type:
rspec
If everything is installed correctly, you should see something like this:
No examples found.
Finished in 0.00005 seconds
0 examples, 0 failures
Time to write some specs!