View Source Mobilizon.Tests.SwooshAssertions (Mobilizon v5.0.0-beta.1)

Assertions for emails.

The assertions provided by this module work by pattern matching against all emails received by the test process against the Swoosh.Email struct. For example:

assert_email_sent %{subject: "You got a message"}

If you want to be additionally explicit, you might:

assert_email_sent %Swoosh.Email{subject: "You got a message"}

If emails are being sent concurrently, you can use assert_email_sending/2:

assert_email_sending %{subject: "You got a message"}

Both functions will return the matched email if the assertion succeeds. You can then perform further matches on it:

email = assert_email_sent %Swoosh.Email{subject: "You got a message"}
assert email.from == {"MyApp", "no-reply@example.com"}

Using pattern matching imposes two limitations. The first one is that you must match precisely the Swoosh.Email structure. For example, the following will not work:

assert_email_sent %{to: "foobar@example.com"}

That's because Swoosh.Email keeps the field as a list. This will work:

assert_email_sent %{to: [{"FooBar", "foobar@example.com"}]}

You are also not allowed to have interpolations. For example, the following will not work:

assert_email_sent %{
  subject: "You have been invited to #{org.name}",
  to: [{user.name, user.email}]
}

However, you can rely on pattern matching and rewrite it as:

email = assert_email_sent %{subject: "You have been invited to " <> org_name}
assert org_name == org.name
assert email.to == [{user.name, user.email}]

Summary

Functions

Matches an email has been sent.

Refutes an email matching pattern has been sent.

Functions

Link to this macro

assert_email_sending(pattern, timeout \\ Application.fetch_env!(:ex_unit, :assert_receive_timeout))

View Source (macro)

Matches an email is sending (within a timeout).

See moduledoc for more information.

Link to this macro

assert_email_sent(pattern)

View Source (macro)

Matches an email has been sent.

See moduledoc for more information.

Link to this macro

refute_email_sent(pattern)

View Source (macro)

Refutes an email matching pattern has been sent.

The opposite of assert_email_sent.