Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refute_email_delivered_with helper, tests #484

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/bamboo/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,41 @@ defmodule Bamboo.Test do
end
end

@doc """
Check that no email was sent with the given parameters

Similarly to `assert_email_delivered_with`, the assertion waits up to 100ms before
failing. Note that you need to send the email again if you want to make other
assertions after this, as this will receive the `{:delivered_email, email}` message.

## Examples

Bamboo.Email.new_email(subject: "something") |> MyApp.Mailer.deliver
refute_email_delivered_with(subject: "something else") # Will pass

email = Bamboo.Email.new_email(subject: "something") |> MyApp.Mailer.deliver
refute_email_delivered_with(subject: ~r/some/) # Will fail
"""
defmacro refute_email_delivered_with(email_params) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!! Thoughts on adding some documentation for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quote bind_quoted: [email_params: email_params] do
import ExUnit.Assertions

received_email_params =
receive do
{:delivered_email, email} -> Map.from_struct(email)
after
100 -> []
end

if is_nil(received_email_params) do
refute false
else
refute Enum.any?(email_params, fn {k, v} -> do_match(received_email_params[k], v) end),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this should be all? instead 🤔 @mtarnovan

Bamboo.Test.flunk_attributes_match(email_params, received_email_params)
end
end
end

@doc false
def do_match(value1, value2 = %Regex{}) do
Regex.match?(value2, value1)
Expand Down Expand Up @@ -263,6 +298,21 @@ defmodule Bamboo.Test do
"""
end

@doc false
def flunk_attributes_match(params_given, params_received) do
"""
The parameters given match.

Parameters given:

#{inspect(params_given)}

Email received:

#{inspect(params_received)}
"""
end

defp delivered_emails do
{:messages, messages} = Process.info(self(), :messages)

Expand Down
41 changes: 41 additions & 0 deletions test/lib/bamboo/adapters/test_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,47 @@ defmodule Bamboo.TestAdapterTest do
end
end

test "refute_email_delivered_with when email does not match" do
mail =
new_email(
to: [nil: "foo@bar.com"],
from: {nil, "baz@bar.com"},
subject: "coffee"
)

TestMailer.deliver_now(mail)
refute_email_delivered_with(subject: ~r/tea/)
refute_email_delivered_with(to: [nil: "something@else.com"])
end

test "refute_email_delivered_with when email matches" do
mail =
new_email(
to: [nil: "foo@bar.com"],
from: {nil, "foo@bar.com"},
subject: "vodka",
text_body: "I really like coffee"
)

TestMailer.deliver_now(mail)

assert_raise ExUnit.AssertionError, fn ->
refute_email_delivered_with(to: mail.to)
end

TestMailer.deliver_now(mail)

assert_raise ExUnit.AssertionError, fn ->
refute_email_delivered_with(subject: mail.subject)
end

TestMailer.deliver_now(mail)

assert_raise ExUnit.AssertionError, fn ->
refute_email_delivered_with(text_body: ~r/coffee/)
end
end

test "assert_no_emails_delivered shows the delivered email" do
sent_email = new_email(from: "foo@bar.com", to: ["foo@bar.com"])

Expand Down