bamboo v0.3.2 Bamboo.Test
Helpers for testing email delivery
Use these helpers with Bamboo.TestAdapter to test email delivery. Typically you’ll want to unit test emails and then in integration tests use helpers from this module to test whether that email was delivered.
In your config
# Typically in config/test.exs
config :my_app, MyApp.Mailer,
adapter: Bamboo.TestAdapter
Unit testing example
You don’t need any special functions to unit test emails.
defmodule MyApp.EmailsTest do
use ExUnit.Case
alias MyApp.Emails
test "welcome email" do
user = %User{name: "John", email: "person@example.com"}
email = Emails.welcome_email(user)
assert email.to == user
assert email.subject == "This is your welcome email"
assert email.html_body =~ "Welcome to the app"
end
end
Integration tests
defmodule MyApp.Email do
import Bamboo.Email
def welcome_email(user) do
new_email(
from: "me@app.com",
to: user,
subject: "Welcome!",
text_body: "Welcome to the app",
html_body: "<strong>Welcome to the app</strong>"
)
end
end
defmodule MyApp.EmailDeliveryTest do
use ExUnit.Case
use Bamboo.Test
alias MyApp.Emails
test "sends welcome email" do
user = %User{...}
email = Emails.welcome_email(user)
email |> MyApp.Mailer.deliver
# Also works with MyApp.Mailer.deliver_later
assert_delivered_email Emails.welcome_email(user)
end
end
Summary
Functions
Checks whether an email was delivered
Checks that no emails were sent
Ensures a particular email was not sent
Macros
Imports Bamboo.Test and Bamboo.Formatter.format_email_address/2
Functions
Checks whether an email was delivered.
Must be used with the Bamboo.TestAdapter or this will never pass. If a Bamboo.Email struct is passed in, it will check that all fields are matching.
You can also pass a keyword list and it will check just the fields you pass in.
Examples
email = Bamboo.Email.new_email(subject: "something")
email |> MyApp.Mailer.deliver
assert_delivered_email(email) # Will pass
assert_delivered_email(subject: "something") # Would also pass
unsent_email = Bamboo.Email.new_email(subject: "something else")
assert_delivered_email(unsent_email) # Will fail