3

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.

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So does that mean I shouldn't use RSpec to create unit tests and functional tests? Essentially your answer is telling me that rspec tests are a separate type of test altogether. Also, with this system of organization, it looks like there's no way to separate rspec tests that test only a single unit from those that serve as larger functional or integration tests. I'd like to be able to maintain this level of separation in my tests. Having a single category of tests isn't acceptable for large projects like the one I'm working on.
Rspec tests that test only your controller or model are unit tests, if they are well-done. They can test whatever you want them to test. When you install the rspec_rails it assumes you want to test your models, views, controllers, and helpers as units. However, you could also create more tests to serve as functional tests. You are not limited to the directories that Rspec creates. You could easily create a "functional" directory and put all your functional tests in there as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.