During development and testing we don’t want to accidentally send emails to the real world. The delivery method for these two envs is set to :test.

    In order to assert that a mailer sent a message, we can look at Hanami::Mailer.deliveries. It’s an array of messages that the framework pretended to deliver during a test. Please make sure to clear them in testing setup.

    1. # spec/bookshelf/mailers/welcome_spec.rb
    2. RSpec.describe Mailers::Welcome do
    3. before { Hanami::Mailer.deliveries.clear }
    4. let(:user) { ... }
    5. it "delivers welcome email" do
    6. Mailers::Welcome.deliver(user: user)
    7. mail = Hanami::Mailer.deliveries.last
    8. expect(mail.to).to eq([user.email])
    9. expect(mail.body.encoded).to eq("Hello, #{ user.name }")
    10. end
    11. end