Last active
December 4, 2020 09:37
-
-
Save tomohiro/dfa7362cd066dd87f25f40bdd6856513 to your computer and use it in GitHub Desktop.
Mail::Matchers example
This file contains hidden or 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 | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'actionmailer', '6.0.3.4' | |
gem 'rspec', '3.10.0' | |
end | |
require 'action_mailer' | |
ActionMailer::Base.delivery_method = :test | |
class NotificationsMailer < ActionMailer::Base | |
default from: '[email protected]' | |
def signup | |
mail(to: '[email protected]', subject: 'Signup') do |format| | |
format.text { render plain: 'Hi' } | |
end | |
end | |
end | |
require 'rspec/autorun' | |
RSpec.configure do |config| | |
config.include Mail::Matchers, type: :mailer | |
end | |
RSpec.describe NotificationsMailer, type: :mailer do | |
before do | |
ActionMailer::Base.deliveries.clear | |
end | |
describe '#signup' do | |
# @see: https://relishapp.com/rspec/rspec-rails/v/3-9/docs/mailer-specs/mailer-spec | |
context 'when using RSpec mailer examples' do | |
subject(:mail) { described_class.signup } | |
it 'renders the headers' do | |
expect(mail.subject).to eq('Signup') | |
expect(mail.to).to eq(['[email protected]']) | |
expect(mail.from).to eq(['[email protected]']) | |
end | |
it 'renders the body' do | |
expect(mail.body.encoded).to match('Hi') | |
end | |
it 'sends the mail' do | |
expect { mail.deliver_now }.to change { ActionMailer::Base.deliveries.count } | |
end | |
end | |
# @see: https://github.com/mikel/mail#using-mail-with-testing-or-specing-libraries | |
context 'when using Mail::Matchers' do | |
subject(:mail) { described_class.signup.deliver_now } | |
it { is_expected.to have_sent_email } | |
it { is_expected.to have_sent_email.from('[email protected]') } | |
it { is_expected.to have_sent_email.to('[email protected]') } | |
it { is_expected.to have_sent_email.with_subject('Signup') } | |
it { is_expected.to have_sent_email.with_body('Hi') } | |
end | |
end | |
end |
Author
tomohiro
commented
Dec 4, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment