minitestの依存関係(?)問題をbundlerで解決する

0pt   2018-11-09 11:49
IT技術情報局

プロを目指す人のためのRuby入門の第3章のminitestのところで、以下のようなテストクラスを用意するところがあります。

require 'minitest/autorun' class FizzBuzzTest < Minitest::Test def test_fizz_buzz assert_equal'1', fizz_buzz(1) assert_equal'2', fizz_buzz(2) assert_equal'Fizz', fizz_buzz(3) end end

以下のようなエラーに遭遇すると思います。

k-3:lib k$ ruby fizz_buzz.rb /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/specification.rb:2291:in `raise_if_conflicts': Unable to activate railties-5.1.6, because activesupport-5.2.1 conflicts with activesupport (= 5.1.6) (Gem::ConflictError) from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/specification.rb:1411:in `activate' from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems.rb:220:in `rescue in try_activate' from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems.rb:213:in `try_activate' from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:126:in `rescue in require' from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:40:in `require' from /usr/local/lib/ruby/gems/2.4.0/gems/railties-5.1.6/lib/minitest/rails_plugin.rb:2:in `<top (required)>' from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:98:in `block in load_plugins' from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:92:in `each' from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:92:in `load_plugins' from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:121:in `run' from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:63:in `block in autorun'

これで解決できます!(本質的な解決ではない)
bunblerを使うことで、Gemfileで指定したバージョンのminitestを実行できる。

gem install bundler bundle init

Gemfileが作られるので、Gemfileに追記する。

# Edit Gemfile gem 'minitest' bundle install bundle exec ruby fizz_buzz.rb

Source: ruby tag

   ITアンテナトップページへ
情報処理/ITの話題が沢山。