Search⌘ K
AI Features

Rails Test Prescriptions, Hold the Rails

Discover how to run tests faster by isolating your code from Rails using RSpec and ActiveRecord. This lesson explains how to bypass loading the entire Rails stack, explicitly load models, manage database connections, and run tests efficiently to improve test speed without compromising accuracy.

RSpec bypassing Rails for speed

The basic idea of isolation is isolating our objects from each other and isolating our code from Rails functionality that would require Rails to be loaded. Isolation means that different objects interact with each other over as small a set of methods as possible. Ideally, it means objects know nothing about the internal structure of other objects in the system.

This doesn’t have to be very complicated. We’ve been writing tests with reasonably good habits so far. To isolate our Project tests from anything other than ActiveRecord, we must change the header. We’ll remove the require rails_helper call and replace it:

Ruby
require_relative "../active_record_test_helper"
require_relative "../../app/models/project"
require_relative "../../app/models/task"

Once we had a single require, now we have ...