LoginSignup
0
0

More than 3 years have passed since last update.

【RSpec】ヘルパーメソッドが使われたviewファイルをテストしたらActionView::Template::Error:が出た。。

Last updated at Posted at 2020-10-10

個人開発のアプリに通知機能を実装し、その後GitにpushしてCircleCIのテスト結果を確認したら突然大量のテストが失敗していて少し困ったので、その解決方法をここで共有させて頂きます。

エラー内容

Failure/Error: -if unchecked_notifications.any?

ActionView::Template::Error:
  undefined local variable or method `unchecked_notifications' for #<#<Class:0x0000555732134140>:0x00005557326f7f78>

通知が来ている場合と来ていない場合で表示するviewを変えるために、unchecked_notificationsという、ヘルパーメソッドを定義しました。しかし、RSpec実行時にはこのヘルパーメソッドが読み込まれておらずエラーが出てテストに失敗してしまったようです。

notifications_helper.rb

module NotificationsHelper
  def unchecked_notifications
    @notifications = current_user.passive_notifications.where(checked: false)
  end
end

解決策

解決策は簡単です。
テストを実行するファイルに、モジュールをインクルードするれば解決するはずです。↓例

post_spec.rb

require 'rails_helper'
#ヘルパーメソッドをインクルード
include NotificationsHelper

RSpec.describe '投稿機能', type: :system do
  #テスト処理
end

最後まで読んでいただきありがとうございます!

日々学習したことをアウトプットしてます!ご指摘などあればコメントいただけますと幸いです!

0
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
0
0