RubyMineでRspec実行時のエラー(Gem::GemNotFoundException)を解決
はじめに
RubyMine上でRspecを実行するとエラーになったので備忘録としてメモしておく。
環境
RubyMine 2017.3.2
Ruby 2.4.3
Rails 5.1.4
エラー内容
RubyMineのRunからrspecを実行すると以下のエラーが発生した。
Testing started at 17:41 ... /Users/xxxxx/.rbenv/versions/2.4.3/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/xxxxx/sample_app/vendor/bundle/ruby/2.4.0/bin/spring rails spec /Users/xxxxx/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems.rb:271:in `find_spec_for_exe': can't find gem spring (>= 0.a) (Gem::GemNotFoundException) from /Users/xxxxx/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems.rb:299:in `activate_bin_path' from /Users/xxxxx/sample_app/vendor/bundle/ruby/2.4.0/bin/spring:23:in `<top (required)>' from -e:1:in `load' from -e:1:in `<main>' Process finished with exit code 1
なお、ターミナルから
% bundle exec rspec
を実行するとエラーは出ない。 ログにはspring gemを見つけることができないというエラーが出ている。Gemfileにはspringが書かれている。
解決方法
RubyMineのメニュー〜Run〜Edit Configurations..を選択する。
specのBundlerタブを選択し、「Run the script in context of the bundle (bundle exec)」にチェックを付ける。
設定後
正常に終了した。
Testing started at 17:58 ... /Users/xxxxx/.rbenv/shims/bundle exec ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/xxxxx/sample_app/vendor/bundle/ruby/2.4.0/bin/spring rails spec /Users/xxxxx/.rbenv/versions/2.4.3/bin/ruby -I/Users/xxxxx/sample_app/vendor/bundle/ruby/2.4.0/gems/rspec-core- 3.7.1/lib:/Users/xxxxx/sample_app/vendor/bundle/ruby/2.4.0/gems/rspec-support-3.7.0/lib /Users/xxxxx/sample_app/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.7.1/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb No examples found. 0 examples, 0 failures, 0 passed Finished in 0.001093 seconds Process finished with exit code 0 Empty test suite.
解析
実行されるコマンドを比較(上:設定変更前、下:設定変更後)
/Users/xxxxx/.rbenv/versions/2.4.3/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/xxxxx/.rbenv/shims/bundle exec ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)
下はRubyMineで設定した通り、bundle execでRubyを実行している。bundle execで実行することで、bundle installでインストールされたgemを参照する。