Rails では、テストでのみメール本文が空になるのはなぜですか? 質問する

Rails では、テストでのみメール本文が空になるのはなぜですか? 質問する

actionmailer クラスとそれに関連するオーバーヘッドがあり、完璧に動作します。しかし、ユニット テスト (rails default minitest) では、メール本文が空です。なぜでしょうか?

私のメーラークラス:

class TermsMailer < ApplicationMailer
  default from: "[email protected]"
  def notice_email(user,filename)
    @user = user
    @file = filename
    mail(to: "[email protected]", subject: 'Data downloaded')
  end
end

私のテスト:

require 'test_helper'

class TermsMailerTest < ActionMailer::TestCase
  test "notice" do
    email = TermsMailer.notice_email(users(:me),'file.ext').deliver_now
    assert_not ActionMailer::Base.deliveries.empty?
    assert_equal ['[email protected]'], email.from
    assert_equal ['[email protected]'], email.to
    assert_equal 'Data downloaded', email.subject
    assert_equal 'arbitrary garbage for comparison', email.body.to_s
  end
end

このメーラーのビューは空白ではなく、正しい内容が実際にメールで送信されます。では、テストでメール本文が空白なのはなぜでしょうか?

ベストアンサー1

おそらく、電子メール テンプレートには 2 つのバージョン (text.erbhtml.erb) があるでしょう。

email.text_part.body.to_sそうであれば、プレーンテキスト メールとemail.html_part.body.to_sHTML バージョンを使用できます。

おすすめ記事