Question

I'm working on a Ruby gem that supports multiple versions of Rails, from at least 3.2 to 4.1 and is tested via minitest with separate gemfiles for different Rails versions. With Rails 4.0 starting to specify a dependency on minitest of ~> 4.2 and in 4.1 bumping that to ~> 5.1, having rails as a dependency seems to dictate the installed version of minitest and the testing setup of the gem itself.

This is a problem because Minitest 5.x is backwards incompatible with 4.x, mainly MiniTest::Unit::TestCase is now Minitest::Test.

I'm looking for an elegant way to maintain compatibility - other gems that I've looked at seem to simply break compatibility, e.g. minitest-rails:

For information on using minitest-rails with Rails 3.0 through 4.0, see the README for the 1.x branch

Two potential solutions are: to redefine the missing class,

unless defined?(MiniTest::Unit::TestCase)
  class MiniTest::Unit::TestCase < Minitest::Test; end
end

or define a common parent class to inherit from,

Testcase = Minitest.const_defined?(:Test) ? Minitest::Test : Minitest::Unit::TestCase

but feedback about pros/cons of either approach or examples of other gems solving this would be great.

Was it helpful?

Solution

Looks like ActiveModel::Serializers defines the missing constant:

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

That seems like a decent approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top