1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

モデルのバリデーションテストがREDにならない問題の解決

1
Last updated at Posted at 2024-01-15

解決したい問題

Railsチュートリアルの6章を進めていて、テキストだとREDになるはずのテストがGREENになってしまう。

原因

どうやらビューに比べてモデルやコントローラの修正は反映されるのに時間がかかるらしい。
実際、他の方でも同じようなことが起きていた。
Rails5でmodelやcontrollerの修正が反映されないやつ

解決した方法

config/environments/development.rbに以下を追加

config.reload_classes_only_on_change = false

ファイルの変更検知の設定

依存関係のあるファイルに変更があった場合に、クラスを再読み込みするようになる
Railsドキュメント

VSCodeを再起動

最初はGitHub Codespacesで進めていたけど、時間制限に引っかかりそうだったので、途中からVSCode+Dockerで進めている。
ローカルだとrails restart、DockerだとDockerの再起動らしいけど、自分の環境ではどっちをやってもダメだったので、VSCodeを一旦閉じてやり直した。

そしたらテストやモデルの変更が反映されていて、無事テストがREDになった。

$ rails test:models     
Running 3 tests in a single process (parallelization threshold is 50)
Started with run options --seed 51857

 FAIL UserTest#test_name_should_be_present (0.01s)
        Expected true to be nil or false
        test/models/user_test.rb:15:in `block in <class:UserTest>'

 FAIL UserTest#test_email_should_be_present (0.02s)
        Expected true to be nil or false
        test/models/user_test.rb:20:in `block in <class:UserTest>'

  3/3: [================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.01843s
3 tests, 3 assertions, 2 failures, 0 errors, 0 skips
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?