springって何?

テストコードにてfactory_botを実装し、rails console で確認したところ、

KeyError: Factory not registered: "user"

が出てきた。

こちらの記事を見るとどうやら"spring"が邪魔をしているらしい。

https://ja.stackoverflow.com/questions/65296/factorybot%E3%81%8C%E5%8B%95%E4%BD%9C%E3%81%97%E3%81%BE%E3%81%9B%E3%82%93

 

spring stop で停止すると問題なくrails console でfactory_bot が動き、レコードが作成できた。

 

[2] pry(main)> FactoryBot.create(:user)

KeyError: Factory not registered: "user"

from /Users/tak/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'

Caused by KeyError: key not found: "user"

from /Users/tak/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'

 

正直このエラーのどこにspring 要素があるのか不明だが、今後コンソールで止まった時はspring を一度停止してみるようにする。

 

 

ちなみに下記のようにspring は確認するとまた動いていた。

tak@MacBook-Air go-events % spring status

Spring is running:

 

12601 spring server | go-events | started 9 mins ago   

12602 ruby -I /Users/tak/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib -I /Users/tak/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib -e require 'spring/application/boot'

 

 

ところで"spring"って何?

 

こちらの記事を見たところ

https://pleiades.io/help/ruby/spring.html

アプリケーションをバックグラウンドで実行し続けることによって開発をスピードアップする Rails アプリケーションプリローダーです。

とのこと。

bin/rails コマンドの2回目以降の起動時間が短縮されるということらしい。

 

この記事には

Test::Unit / Shoulda / Minitest テストは、デフォルトで Spring を使用せずに実行されることに注意してください。

と書いてあるからspring 止めなきゃいけなかったのかな?

 

とりあえずなんとなくわかった…ということにしておこう。