Nikita Fedyashev
Nikita Fedyashev

Reputation: 18983

How to test that all ActionMailer preview actions render/do not fail?

My app has a lot of mailer actions and previews and I would like to automatically test that all listed mailer preview links respond with HTTP=200/do not fail rendering.

This should solve the problem of duplication between mailer preview actions and specs.

How to do it?

Upvotes: 0

Views: 23

Answers (1)

Nikita Fedyashev
Nikita Fedyashev

Reputation: 18983

The following solution is not the most optimal(e.g. additional request per each mailer + requires rack_test) but it works:

# cat spec/features/mailer_preview_spec.rb

require 'rails_helper'

RSpec.describe 'MailerPreview', type: :feature do
  before do
    # Assuming you have the Capybara setup
    Capybara.default_driver = :rack_test
  end

  it 'opens /rails/mailers and checks each link' do
    visit '/rails/mailers'
    expect(page.status_code).to eq(200)

    links = page.all('a').map { |link| link[:href] }
    links.each do |link|
      expect {
        visit link
      }.not_to raise_error, "Expected #{link} not to fail"
    end
  end
end

As a reminder, make sure config.action_mailer.show_previews = true is set in test environment for this to work.

Upvotes: 0

Related Questions