Last active
November 20, 2017 10:17
-
-
Save peter/9596595 to your computer and use it in GitHub Desktop.
Faster Unit Testing in Rails Without Loading Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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