Skip to content

Instantly share code, notes, and snippets.

@usutani
Last active April 30, 2023 08:31
Show Gist options
  • Save usutani/7fdbbc9226cc5addb9d513da4d55e7dc to your computer and use it in GitHub Desktop.
Save usutani/7fdbbc9226cc5addb9d513da4d55e7dc to your computer and use it in GitHub Desktop.
Action Mailerのテスト用テンプレート
# 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: "from@example.com", to: "to@example.com", 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 ["from@example.com"], email.from
assert_equal ["to@example.com"], 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