--- 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_generatorOnce the gem is installed, you must configure you Rails app. Stand in the root of your Rails app and run:
ruby script/generate rspecNow, you can generate models and controllers in a similar fashion to Rails' builtin generators. Example:
ruby script/generate rspec_model personor
ruby script/generate rspec_controller personFor more information on each generator, just run them without any arguments. h3. Running specs All specs can be run with
rake specModel specs can be run with
rake spec:modelsController specs can be run with
rake spec:controllersTo see all the RSpec related tasks, run
rake --tasks spech3. 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: