Rails 6.1 + Ruby 3.0.2でテスト時にエラーがでる

Ruby on Rails チュートリアル 第6版 第3章のテストの節を実行していたら、以下のエラーに直面した。やっぱり、ちゃんとチュートリアルどおり、指定されたバージョンでやらないとだめよね。


  • チュートリアルで推奨されている環境:Ruby 2.7 + Rails 6.0.0
  • 私の環境:Ruby 3.0 + Rails 6.1

現象:cannot load such file -- rexml/document (LoadError)

% rails test
/Users/gotoh/.rbenv/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:34:in `require': cannot load such file -- rexml/document (LoadError)
	from /Users/gotoh/.rbenv/gems/3.0.0/gems/activesupport- `block in require'
	from /Users/gotoh/.rbenv/gems/3.0.0/gems/activesupport- `load_dependency'
	from /Users/gotoh/.rbenv/gems/3.0.0/gems/activesupport- `require'


rexml gem is a bundled gem since Ruby 3.0.0. So you must add rexml to Gemfile.

On Ruby 2.6 and 2.7, rexml gem is default gem. So default rexml is used if Gemfile does not include rexml. On these versions of ruby, you must add rexml to Gemfile only if you want to use specific rexml version.
(Rails 6.1, Ruby 3.0.0: tests error as they cannot load rexml - Stack Overflowより)



gem 'rexml'


% bundle install


% rails test
Running via Spring preloader in process 99508
Run options: --seed 58235

# Running:


Finished in 3.176816s, 0.6296 runs/s, 0.6296 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips