--- title: RSpec on Rails inMenu: true --- h2. RSpec on Rails RSpec's rspec_generator Rubygem brings RSpec to Rails. h3. Features * Integrated fixture loading * Uses many of the controller-test integration features * Special generators for models and controllers that generate specs instead of tests. h3. Installation RSpec support for rails lives in a separate gem. Install that gem:
gem install rspec_generator
Once the gem is installed, you must configure you Rails app. Stand in the root of your Rails app and run:
ruby script/generate rspec
Now, you can generate models and controllers in a similar fashion to Rails' builtin generators. Example:
ruby script/generate rspec_model person
or
ruby script/generate rspec_controller person
For more information on each generator, just run them without any arguments. h3. Running specs All specs can be run with
rake spec
Model specs can be run with
rake spec:models
Controller specs can be run with
rake spec:controllers
To see all the RSpec related tasks, run
rake --tasks spec
h3. Naming conventions When you use Rails without RSpec (with Test::Unit), tests for models end up in tests/unit and tests for controllers end up in tests/functional. In order to make things more consistent, RSpec chooses a slightly different naming convention for direcotries and Rake tasks. So you will find model specs under specs/models, and controller specs under specs/controllers. The Rake tasks are named accordingly. h3. Examples RSpec on Rails adds several methods to your specs with a look and feel similar to Test::Unit. Example: Model: Controller: h3. Translating existing Test::Unit tests The test2spec tool that ships with RSpec translates existing tests into RSpec specs. Translating tests to specs in a Rails environment requires some manual steps... h4. Install the rspec_generator How to do this is described above h4. Modify your test/test_helper.rb In order to be able to translate any Rails tests, you must modify your test/test_helper.rb file:
# This line must be commented out in order for test2spec to work.
# require 'test_help'
require 'test2spec_help'
The reason for this is that the test_help mixin confuses test2spec to the point where it's unable to perform the translation. The test2spec_help addresses this shortcoming. h4. Perform the translations Now you can translate your unit tests (model tests) with:
test2spec --template spec/test2spec.erb --specdir spec/models test/unit
and your functional tests (controller tests) with:
test2spec --template spec/test2spec.erb --specdir spec/controllers test/functional
h4. Edit your translated specs test2spec currently doesn't translate class-level statements such as fixtures, so you have to do this yourself. Copy all the fixtures statements in your tests to the corresponding contexts. Example: context "The Foo Model" do fixtures :foo end h4. Make sure fixtures are found. By default, RSpec on Rails expects to find fixtures under spec/fixtures. You should either move your existing test/fixtures/*.yml files to spec/fixtures or edit your spec/spec_helper.rb to point to the old test/fixtures location. Beware that every time you do a script/generate rspec_model, new fixstures will always be written to spec/fixtures.