Jeff
Jeff

Reputation: 16031

How should I integrate RSpec with Rake, Rails and my existing tests?

I'd like to start using RSpec to write tests for my Rails app, but I have a lot of existing tests written using Test::Unit. I'd like to continue being able to run all of the tests (or all of the unit tests, functional tests, etc.) simply from the command line, with something like

rake test:units

or whatever, and have it run all unit tests, whether they're written with Test::Unit or RSpec. And I definitely don't want to waste a bunch of time converting my existing tests to RSpec; that's a non-starter.

Right now I'm thinking that I'd like my RSpec tests to exist right alongside my existing tests, in test/unit, test/functional, etc. What is the best way to accomplish this? Or is this even a good idea at all? That is, I'm pretty new to RSpec (and Ruby and Rails, for that matter), so maybe it's a better practice to keep the RSpecs separate. Regardless of where they're stored on the filesystem, though, I'll still need to be able to run all tests with a single rake task, and I'll need to collect code coverage numbers for the entire test corpus using rcov, which I'm already doing with my existing tests.

Upvotes: 3

Views: 2368

Answers (1)

Jamison Dance
Jamison Dance

Reputation: 20174

edit: fixed some wrong statements about Rspec rails directory structure.


Rspec usually puts its tests in a separate directory structure, spec/, which looks like this:

spec/
├── controllers
│   ├── pages_controller_spec.rb
│   └── users_controller_spec.rb
├── helpers
│   ├── pages_helper_spec.rb
│   └── users_helper_spec.rb
├── models
│   └── user_spec.rb
├── spec_helper.rb
└── views
    ├── pages
    │   └── home.html.haml_spec.rb
    └── users
        ├── index.html.haml_spec.rb
        └── show.html.haml_spec.rb

It has a separate directory for models, controllers, and views. I am not sure exactly how Test::Unit structures its directories, but this is the default directory structure for Rspec. Note the spec_helper.rb file. This file tells Rspec where to look for specs, and has a few other configuration options.

Your best bet is probably just to follow the rspec best practices for having a spec dir, and make your own rake task that runs both the unit tests and the rspec tests. Something like this:

task :run_tests do
  system("rspec spec/")
  system("rake test:units")
end

That will run your rspec tests and then your Test::Unit tests in turn.

Upvotes: 6

Related Questions