RubyMineでRspecのテストの自動実行を設定してみた


## 背景
RubyMineを利用しているので、設定について試してみました。

## 環境
```shell
❯ sw_vers
ProductName:	macOS
ProductVersion:	12.3.1
BuildVersion:	21E258
```

```
RubyMine 2023.2.1
```

## 設定
- `Edit Configurations`をクリックし、設定を追加
  ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37718/9d925353-6be6-0caf-8109-bbef30874e84.png)
- Rspecの設定を新たに追加
  - NameとSpecs folderを指定
  ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37718/5ae82201-52fe-6a40-ae05-3b8460eb3c68.png)
- 実行したところ、以下のエラーが表示された 

```ruby
/bin/zsh -c "bash -c 'env RBENV_VERSION=3.0.2 /usr/local/Cellar/rbenv/1.2.0/libexec/rbenv exec ruby /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/bin/rspec /Users/feng.zhang/rfs/spec --require teamcity/spec/runner/formatter/teamcity/formatter --format '\"'\"'Spec::Runner::Formatter::TeamcityFormatter'\"'\"' --pattern '\"'\"'**/*_spec.rb'\"'\"''"
Testing started at 16:42 ...
/Users/feng.zhang/.rbenv/versions/3.0.2/lib/ruby/3.0.0/rubygems.rb:281:in `find_spec_for_exe': can't find gem rspec-core (>= 0.a) with executable rspec (Gem::GemNotFoundException)
	from /Users/feng.zhang/.rbenv/versions/3.0.2/lib/ruby/3.0.0/rubygems.rb:300:in `activate_bin_path'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/bin/rspec:23:in `<main>'

Process finished with exit code 1
```

- specが見つからないため、設定を行う
  - ./bin/rspecを指定 
  ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37718/81a705d9-449a-fc7d-5a79-3119308c9d8b.png)
- さらに別のエラーが発生

```ruby
/bin/zsh -c "bash -c 'env RBENV_VERSION=3.0.2 /usr/local/Cellar/rbenv/1.2.0/libexec/rbenv exec ruby /Users/feng.zhang/rfs/bin/rspec /Users/feng.zhang/rfs/spec --require teamcity/spec/runner/formatter/teamcity/formatter --format '\"'\"'Spec::Runner::Formatter::TeamcityFormatter'\"'\"' --pattern '\"'\"'**/*_spec.rb'\"'\"''"
Testing started at 21:50 ...
Warning: the running version of Bundler (2.2.22) is older than the version that created the lockfile (2.4.12). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.4.12`.
/Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:76:in `spawn': No such file or directory - spring (Errno::ENOENT)
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:76:in `boot_server'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:56:in `cold_run'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:33:in `rescue in call'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:30:in `call'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/command.rb:7:in `call'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client.rb:30:in `run'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/bin/spring:49:in `<top (required)>'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/binstub.rb:11:in `load'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/binstub.rb:11:in `<top (required)>'
	from <internal:/Users/feng.zhang/.rbenv/versions/3.0.2/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from <internal:/Users/feng.zhang/.rbenv/versions/3.0.2/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from /Users/feng.zhang/rfs/bin/spring:12:in `block in <top (required)>'
	from <internal:kernel>:90:in `tap'
	from /Users/feng.zhang/rfs/bin/spring:9:in `<top (required)>'
	from /Users/feng.zhang/rfs/bin/rspec:4:in `load'
	from /Users/feng.zhang/rfs/bin/rspec:4:in `<main>'
/Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:26:in `initialize': No such file or directory - connect(2) for /private/var/folders/xr/yldgyptn0s72bcrmnwxpf_kh0000gp/T/rmcbab4a02rfs/4150fadaa77507282be06c8a87a8828f (Errno::ENOENT)
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:26:in `open'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:26:in `connect'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/run.rb:31:in `call'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client/command.rb:7:in `call'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/client.rb:30:in `run'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/bin/spring:49:in `<top (required)>'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/binstub.rb:11:in `load'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/gems/spring-4.0.0/lib/spring/binstub.rb:11:in `<top (required)>'
	from <internal:/Users/feng.zhang/.rbenv/versions/3.0.2/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from <internal:/Users/feng.zhang/.rbenv/versions/3.0.2/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from /Users/feng.zhang/rfs/bin/spring:12:in `block in <top (required)>'
	from <internal:kernel>:90:in `tap'
	from /Users/feng.zhang/rfs/bin/spring:9:in `<top (required)>'
	from /Users/feng.zhang/rfs/bin/rspec:4:in `load'
	from /Users/feng.zhang/rfs/bin/rspec:4:in `<main>'

Process finished with exit code 1
```

- springも指定する

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37718/2c775a5b-ab14-40d9-47d7-a7f14c285c6c.png)

- それでもエラーが発生

```ruby
/bin/zsh -c "bash -c 'env RBENV_VERSION=3.0.2 /usr/local/Cellar/rbenv/1.2.0/libexec/rbenv exec ruby /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/bin/spring rspec /Users/feng.zhang/rfs/spec --require teamcity/spec/runner/formatter/teamcity/formatter --format '\"'\"'Spec::Runner::Formatter::TeamcityFormatter'\"'\"' --pattern '\"'\"'**/*_spec.rb'\"'\"''"
Testing started at 21:53 ...
/Users/feng.zhang/.rbenv/versions/3.0.2/lib/ruby/3.0.0/rubygems.rb:281:in `find_spec_for_exe': can't find gem spring (>= 0.a) with executable spring (Gem::GemNotFoundException)
	from /Users/feng.zhang/.rbenv/versions/3.0.2/lib/ruby/3.0.0/rubygems.rb:300:in `activate_bin_path'
	from /Users/feng.zhang/rfs/vendor/bundle/ruby/3.0.0/bin/spring:23:in `<main>'

Process finished with exit code 1
```

- bundleの設定が誤っている可能性が考えられる

```
❯ bundle --version
Bundler version 2.3.10

❯ tail -n 3 Gemfile.lock

BUNDLED WITH
   2.4.12
```

- bundleのバージョンを揃える

```
❯ gem uninstall bundler
Gem bundler-2.2.22 cannot be uninstalled because it is a default gem
Successfully uninstalled bundler-2.3.10

~
❯ gem list | grep bundler
bundler (default: 2.2.22)

❯ gem install bundler:2.4.12
Fetching bundler-2.4.12.gem
Successfully installed bundler-2.4.12
Parsing documentation for bundler-2.4.12
Installing ri documentation for bundler-2.4.12
Done installing documentation for bundler after 0 seconds
1 gem installed
```

- 再度テストを実行すると、正常に動作した

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37718/9e1c7b17-1d2a-86ec-9749-b2af264bfe9f.png)

## その他
- auto test deplyの設定

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37718/ea3f07e7-f654-3345-d3d1-1c69f3e21aa6.png)

- **auto testを有効にする**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37718/c10c1955-6dfa-4713-20a8-b3b8a523a293.png)

## 参照
- [Run tests \| RubyMine Documentation](https://www.jetbrains.com/help/ruby/performing-tests.html#rerun)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37718/7bab4aa9-f379-021d-90dd-fbf74bf841d7.png)