[Ruby on Rails 5] CLI と Rubymine 上で RSpec を実行するための設定

Rails 5.2.4.1 にアップグレード後、`bundle exec rspec` で実行できるテストが `bin/rails spec` で実行できなくなりました。
Rubymine に設定していた `Run` rake task の設定も同様でしたので調査。

## CLI での実行

`bin/rspec` が公式に書かれているので、これを設定していきます。

> – [rspec/rspec-rails: RSpec for Rails-3+](https://github.com/rspec/rspec-rails#running-specs)

`bin/rspec` は存在しません。

“`prettyprinted
% ls bin
bundle* rails* rake* setup* update* yarn*
“`

`bundle binstubs rspec-core` を実行し、`bin/rspec` をしたところ警告が表示されました。

“`prettyprinted
% bundle binstubs rspec-core
% bin/rspec
Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler –force`, then run this command again.
“`

と言われるので下記を実行したところ、`bin/rspec` でテストが実行できるようになりました。

“`prettyprinted
% bundle binstubs bundler –force
“`

ただプロジェクトの `bin/bundle` を確認したところ、ファイルの内容が大きく変更されていました。

おそらく下記を実行すべきだったようなので、`bin/bundle` ファイルの内容も元に戻しました。

“`prettyprinted
% bin/bundle binstubs rspec-core
“`

違うのかな。

“`prettyprinted
% which bundle
/Users/foo/.rbenv/shims/bundle
% which bin/bundle
bin/bundle
“`

> – [Rails’ binstubs vs. Bundler binstubs – incompatibility and breaking changes, maybe?](https://gist.github.com/stevenharman/8fca435526917a70ec57ec457ecd55c9)
> – [Rails-generated binstubs are incompatible with Bundler-generated binstubs · Issue #31193 · rails/rails](https://github.com/rails/rails/issues/31193)

## Rubymine での実行

`Run` メニューから実行するもテストが走りません。

“`prettyprinted
Testing started at 3:10 …
/usr/local/bin/zsh -c “bash -c ‘env RBENV_VERSION=2.6.5 /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv exec bundle exec ruby /Users/foo/projects/bar/vendor/bundle/ruby/2.6.0/bin/spring rails spec'”

Process finished with exit code 0
“`

修正のついでに `bin/spring` の設定を行います。

> – [rails/spring: Rails application preloader](https://github.com/rails/spring)

“`prettyprinted
% bundle exec spring binstub –all
* bin/rake: Spring inserted
* bin/rails: Spring inserted
% ls bin
bundle* rails* rake* rspec* setup* spring* update* yarn*
“`

元の設定から `Environment Valuables` に `RAILS_ENV=test` 値を与えると動きました。

`Run` メニューから実行した結果に表示されるコマンド上は何も変わっていないのがよく分かりません。

“`prettyprinted
/usr/local/bin/zsh -c “bash -c ‘env RBENV_VERSION=2.6.5 /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv exec bundle exec ruby /Users/foo/projects/bar/vendor/bundle/ruby/2.6.0/bin/spring rails spec'”
“`

RSpec で設定した場合は、このような形になりました。

表示されるコマンド。

“`prettyprinted
/usr/local/bin/zsh -c “bash -c ‘env RBENV_VERSION=2.6.5 /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv exec bundle exec ruby /Users/foo/projects/bar/vendor/bundle/ruby/2.6.0/bin/rspec /Users/foo/projects/bar/spec –require teamcity/spec/runner/formatter/teamcity/formatter –format ‘\”‘\”‘Spec::Runner::Formatter::TeamcityFormatter’\”‘\”‘ –pattern ‘\”‘\”‘**/*_spec.rb’\”‘\”””
“`

## 補遺

`binstub` の話。

> – [【翻訳+解説】binstubをしっかり理解する: RubyGems、rbenv、bundlerの挙動|TechRacho(テックラッチョ)〜エンジニアの「?」を「!」に〜|BPS株式会社](https://techracho.bpsinc.jp/hachi8833/2016_08_24/25037)

`binstub` とチーム開発の話。

> – [rails new するとできる bin ディレクトリまとめ – Qiita](https://qiita.com/ayasuda/items/7edcef9f9508d35cb3dd)

`script/rails` をやめて `bin/rails` になったあたりの話。

> – [Rails 4.0 と bundler install –binstubs について – おもしろwebサービス開発日記](https://blog.willnet.in/entry/2013/03/30/220603)