rspecでDevise::MissingWardenエラーが出た時の解決策

エラー

rails6で、認証にdeviceを使用しているアプリケーションで、rspecを実行した際に、以下のエラーが発生。

     Devise::MissingWarden:
       Devise could not find the `Warden::Proxy` instance on your request environment.
       Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
       If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.

解決策

rails_helper.rbに以下を追記すると解消します。

config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view