Blank?=False

ゆるゆる仕事したいフリーランスエンジニアの記事

Railsのテストを実行しようとすると`Web Console is activated in the test environment`と怒られた話

f:id:stonebeach-dakar:20160618082622p:plain

Railsのテストをを実行しようとrails testとターミナルに打ったらこんな感じのメッセージが出てきて怒られました。

Web Console is activated in the test environment, which is
usually a mistake. To ensure it's only activated in development
mode, move it to the development group of your Gemfile:

    gem 'web-console', group: :development

If you still want to run it the test environment (and know
what you are doing), put this in your Rails application
configuration:

    config.web_console.development_only = false

解決方法

メッセージに書いてあるとおり、Gemfileでweb-consoleの宣言を開発環境のみ使えるように書き換えます。

Gemfile

<省略>
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'pry-byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

<省略>
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'pry-byebug'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
end

そもそもweb-consoleってどんなGem?

このWeb-Console,デフォルトで入るGemなのでどんなGemなのか知らなかったので調べてみた。

Railsのweb-consoleについて | 日々雑記


エラー画面の時に下に出てくるコンソールがWeb-Consoleみたい。
f:id:stonebeach-dakar:20160629054729p:plain

リポジトリはここ。
github.com
上の解説ページではdevelopmenttest環境で動く、とあるけど、Githubのreadmeを見ると、

Why I'm getting an undefined method web_console?

Make sure you configuration lives in config/environments/development.rb.

web_consoleが未定義のメソッドと怒られるけどなんで?という質問に、環境がdevelopmentか確認してね、と回答されているので、基本的にdevelopmentオンリーのようです。


エラーメッセージを見ると、config.web_console.development_only = falseと設定すれば、他の環境でも使えそうな感じですが・・・コレはまた後で試してみよう。