Last active
April 30, 2023 08:31
-
-
Save usutani/7fdbbc9226cc5addb9d513da4d55e7dc to your computer and use it in GitHub Desktop.
Action Mailerのテスト用テンプレート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# https://github.com/rails/rails/blob/main/guides/bug_report_templates/generic_gem.rb | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
# Activate the gem you are reporting the issue against. | |
gem "actionmailer", "~> 7.0.0" | |
end | |
require "action_mailer" | |
require "minitest/autorun" | |
require "logger" | |
ActionMailer::Base.logger = Logger.new(STDOUT) | |
class UserMailer < ActionMailer::Base | |
def welcome | |
@name = params[:name] | |
mail(from: "[email protected]", to: "[email protected]", subject: "subject") do |format| | |
format.text { render plain: "Hello #{@name}!" } | |
end | |
end | |
end | |
class BugTest < ActionMailer::TestCase | |
def test_deliver_now | |
email = UserMailer.with(name: "Mikel").welcome | |
assert_emails 1 do | |
email.deliver_now | |
end | |
assert_equal ["[email protected]"], email.from | |
assert_equal ["[email protected]"], email.to | |
assert_equal "subject", email.subject | |
assert_equal "Hello Mikel!", email.body.to_s | |
end | |
def test_deliver_later | |
assert_enqueued_emails 2 do | |
UserMailer.with(name: "Mikel").welcome.deliver_later(wait_until: 10.hours.from_now) | |
UserMailer.with(name: "Mikel").welcome.deliver_later(wait: 1.hour) | |
end | |
assert_emails 0 | |
# https://github.com/rails/rails/pull/47520 | |
# deliver_enqueued_emails | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment