如何测试所有 ActionMailer 预览操作渲染/不失败?

问题描述 投票:0回答:1

我的应用程序有很多邮件程序操作和预览,我想自动测试所有列出的邮件程序预览链接是否以 HTTP=200 响应/不会渲染失败。

这应该可以解决邮件预览操作和规范之间重复的问题。

如何做?

rspec-rails actionmailer
1个回答
0
投票

以下解决方案不是最优的(例如,每个邮件程序的附加请求+需要rack_test),但它有效:

# 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

提醒一下,请确保将

config.action_mailer.show_previews = true
设置在
test
环境中才能正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.