[RSpec] When I tested the view file that used the helper method, I got ActionView :: Template :: Error :. ..

I implemented a notification function in my personally developed app, then pushed it to Git and checked the CircleCI test results, but suddenly a lot of tests failed and I was a little troubled, so I will share the solution here.

error contents

Failure/Error: -if unchecked_notifications.any?

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

I have defined a helper method called unchecked_notifications to change the view displayed when notifications are coming and when notifications are not coming. However, when RSpec is executed, this helper method is not loaded and it seems that the test failed with an error.

notifications_helper.rb



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

solution

The solution is simple. Include the module in the file where you want to run the test and it should work. ↓ Example

post_spec.rb



require 'rails_helper'
#Include helper methods
include NotificationsHelper

RSpec.describe 'Posting function', type: :system do
  #Test process
end

Thank you for reading to the end!

I output what I learned every day! If you have any suggestions, I would appreciate it if you could comment!

Recommended Posts

[RSpec] When I tested the view file that used the helper method, I got ActionView :: Template :: Error :. ..
In WSL2, when I did `docker-compose up`, I got an error saying that the sh file was not found.
rspec Error: ActionView :: Template :: Error: undefined method `name'for nil: NilClass
[Rails] "pry-rails" that can be used when saving with the create method
[RSpec] What I got stuck when I wrote the mailer (password reset process) test
I got an "ActionView :: Template :: Error: Permission denied" error in the test of 3.3.1 of Rails tutorial 6th edition, so I solved it.
I got an error when I ran rake routes.
Rspec: I want to test the post-execution state when I set a method on subject
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec