Running Rails-free Tests

Learn to run tests in separate directories for fast tests.

If we have some tests that run without loading the Rails environment, we need to be careful to run them separately from the slower, Rails-loading tests. We lose the benefit of not loading Rails if some of our tests do load Rails. Also, if we’re doing any namespace munging in our fast tests, mixing and matching fast and slow tests are guaranteed to give weird errors.

Separate directories for fast tests

The easiest way to manage running fast tests is to keep them in their own directory or directories and pass those directories as arguments to ruby. This is easy in RSpec since the spec command will take directories or globs as arguments.

For example, in the following terminal, we are running tests with separate directories, using the bundle exec rspec spec/models spec/workflows command.

Get hands-on with 1400+ tech skills courses.