ruby-on-railsrspecdeviserspec-railssystem-testing

Devise-related system tests fail when run en mass but they pass when run individually (Devise::Mailer)


I'm using RSpec to system test my Ruby on Rails 7 app that uses Devise.

When I run system specs individually then each of them passes, but when I run all system specs globally en mass then the ones related to Devise (i.e. confirmations, passwords, registrations, sessions, and unlocks) fails with a messages like

ActionView::Template::Error: undefined local variable or method `controller' for #<Devise::Mailer:0x0000000007fb98>

Then, if I (re)run only the failed tests en mass then they all pass.

All failing Device-related (i.e. failing Devise::Mailer-related) system tests are like the following (below is one for user registrations; the others are similar but specialized for confirmations, passwords, etc.):

RSpec.describe 'Registrations: signing up user', :type => :system do
  describe 'sign up user' do
    before(:each) do
      @regular_user = FactoryBot.build(:regular_user)

      visit new_user_registration_path
    end

    it 'should sign up user', :js => true do
      within("#form_id") do
        fill_in_sign_up_user_form(@regular_user.username, @regular_user.email, @regular_user.password)
      end

      expect(page).to have_current_path(root_path)

      # ...
    end
  end
end

What's the problem with the Devise::Mailer running en mass and how to solve it?


Solution

  • The problem seems to be solved by adding ActiveJob::TestHelper in the rails_helper.rb file:

    # rails_helper.rb
    
    # ...
    RSpec.configure do |config|
      # ...
      config.include ActiveJob::TestHelper
      # ...
    end