Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Faster Unit Testing in Rails Without Loading Rails
# Running a single minimalistic unit test with Rails 4 and Ruby 2.1.1 is a lot faster
# if you avoid loading Rails:
# Running the test *with* Rails
time ruby -Itest test/models/my_model_test.rb # => real ~ 6s
# Running the test *without* Rails
time ruby -Itest/no_rails test/models/my_model_test.rb # => real ~ 0.6s
# app/models/my_model.rb (your application logic that doesn't depend on Rails)
class MyModel
def self.foobar
"foobar"
end
end
# test/no_rails/test_helper.rb
require 'bundler/setup'
require 'minitest/unit'
require 'minitest/autorun'
# test/models/my_model_test.rb
require 'test_helper'
require File.expand_path('../../../app/models/my_model', __FILE__)
class MyModelTest < MiniTest::Unit::TestCase
def test_foobar
assert_equal "foobar", MyModel.foobar
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment