Spec::Rails
A Rails plugin that brings RSpec to Rails.
Features
- Use RSpec to independently specify models, views, controllers and helpers.
- Integrated fixture loading.
- Special generators for models and controllers that generate specs instead of tests.
- Special RSpec matchers for even more readable specs.
Vision
For people for whom TDD is a brand new concept, the testing support built into Rails is a huge
leap forward. The fact that it’s built right in is fantastic, and Rails
apps are generally much easier to maintain than they would have been without such support.
For those of us coming from a history with TDD, and now BDD, the existing support
presents some problems related to dependencies across specs. To that end, RSpec
supports 4 types of specs. This is largely inspired by Test::Rails, which is
the rails testing framework built into ZenTest.
We’ve also built in first class mocking and stubbing support in order to
break dependencies across these different concerns.
Different Types of Example Groups
Spec::Rails supports different ExampleGroup subclasses for the following types of specs:
Model Examples
These are the equivalent of unit tests in Rails’ built in testing. Ironically
(for the traditional TDD’er) these are the only specs that we feel should actually interact
with the database. Learn more ….
Controller Examples
These align somewhat with functional tests in rails, except
that they do not actually render views (though you can force rendering of views
if you prefer). Instead of setting expectations about what goes on a page, you
set expectations about what templates get rendered. Learn more….
View Examples
This is the other half of Rails’ functional testing. View specs allow
you to set up assigns (thanks to ZenTest). Learn more ….
Helper Examples
… let you specify directly methods that live in your helpers. Learn more ….
Fixtures
You can use fixture in any of your specs, be they model, view, controller, or helper. If you have fixtures that you want to use
globally, you can set them in spec/spec_helper.rb. See that file for more information.
Naming conventions
For clarity and consistency, RSpec uses a slightly different naming convention
for directories and Rake tasks than what you get from the Test::Unit testing built into rails.
project
|
+--app
|
+--...
|
+--spec
|
+-- spec_helper.rb
|
+-- controllers
|
+-- helpers
|
+-- models
|
+-- views
The Rake tasks are named accordingly.