Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasresende/18c2eedd886a52392f21b61e969080ea to your computer and use it in GitHub Desktop.
Save douglasresende/18c2eedd886a52392f21b61e969080ea to your computer and use it in GitHub Desktop.
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