Skip to content

Commit 9a81c99

Browse files
committed
use rspec-rails logic for test unit assertions
1 parent e8a2b2d commit 9a81c99

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ group :documentation do
2020
gem 'github-markup', '1.0.0'
2121
end
2222

23-
case version = ENV.fetch('RAILS_VERSION', '4.0.2')
23+
case version = ENV.fetch('RAILS_VERSION', '4.2.4')
2424
when /\Amaster\z/, /stable\z/
2525
gem "activerecord", :github => "rails/rails", :branch => version
2626
gem "activemodel", :github => "rails/rails", :branch => version
@@ -31,4 +31,6 @@ else
3131
gem "activesupport", version
3232
end
3333

34+
gem "test-unit", '~> 3' if version >= '3.2.22' && version < '4.0.0'
35+
3436
gem "i18n", '< 0.7.0' if RUBY_VERSION < '1.9.3'

spec/rspec/active_model/mocks/mock_model_spec.rb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,45 @@
414414
include Minitest::Assertions
415415
include MinitestAssertion
416416
rescue LoadError
417-
require 'test/unit/assertions'
418-
include Test::Unit::Assertions
417+
if RUBY_VERSION >= '2.2.0'
418+
# Minitest / TestUnit has been removed from ruby core. However, we are
419+
# on an old Rails version and must load the appropriate gem
420+
version = ENV.fetch('RAILS_VERSION', '4.2.4')
421+
if version >= '4.0.0'
422+
# ActiveSupport 4.0.x has the minitest '~> 4.2' gem as a dependency
423+
# This gem has no `lib/minitest.rb` file.
424+
gem 'minitest' if defined?(Kernel.gem)
425+
require 'minitest/unit'
426+
Assertions = MiniTest::Assertions
427+
elsif version >= '3.2.22'
428+
begin
429+
# Test::Unit "helpfully" sets up autoload for its `AutoRunner`.
430+
# While we do not reference it directly, when we load the `TestCase`
431+
# classes from AS (ActiveSupport), AS kindly references `AutoRunner`
432+
# for everyone.
433+
#
434+
# To handle this we need to pre-emptively load 'test/unit' and make
435+
# sure the version installed has `AutoRunner` (the 3.x line does to
436+
# date). If so, we turn the auto runner off.
437+
require 'test/unit'
438+
require 'test/unit/assertions'
439+
rescue LoadError => e
440+
raise LoadError, <<-ERR.squeeze, e.backtrace
441+
Ruby 2.2+ has removed test/unit from the core library. Rails
442+
requires this as a dependency. Please add test-unit gem to your
443+
Gemfile: `gem 'test-unit', '~> 3.0'` (#{e.message})"
444+
ERR
445+
end
446+
Assertions = Test::Unit::Assertions
447+
else
448+
raise LoadError, <<-ERR.squeeze
449+
Ruby 2.2+ doesn't support this version of Rails #{version}
450+
ERR
451+
end
452+
else
453+
require 'test/unit/assertions'
454+
include Test::Unit::Assertions
455+
end
419456
end
420457

421458
require 'active_model/lint'

0 commit comments

Comments
 (0)