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

Use a formatter for assert_email_delivered_with/1 #482

Closed
Closed
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
19 changes: 15 additions & 4 deletions lib/bamboo/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ defmodule Bamboo.Test do
unsent_email = Bamboo.Email.new_email(subject: "something else")
assert_email_delivered_with(subject: "something else") # Will fail

The function will use the Bamboo Formatter when checking email addresse.s
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
The function will use the Bamboo Formatter when checking email addresse.s
The function will use the Bamboo Formatter when checking email addresses.


email = Bamboo.Email.new_email(to: "someone@example.com")
email |> MyApp.Mailer.deliver
assert_email_delivered_with(to: "someone@example.com") # Will pass

You can also pass a regex to match portions of an email.

## Example
Expand All @@ -198,19 +204,24 @@ defmodule Bamboo.Test do

received_email_params = email |> Map.from_struct()

assert Enum.all?(email_params, fn {k, v} -> do_match(received_email_params[k], v) end),
assert Enum.all?(email_params, fn {k, v} -> do_match(received_email_params[k], v, k) end),
Bamboo.Test.flunk_attributes_do_not_match(email_params, received_email_params)
end
end

@doc false
def do_match(value1, value2 = %Regex{}) do
def do_match(value1, value2 = %Regex{}, _type) do
Regex.match?(value2, value1)
end

@doc false
def do_match(value1, value2) do
value1 == value2
def do_match(value1, value2, type) do
value1 == value2 || value1 == format(value2, type)
end

@doc false
defp format(record, type) do
Bamboo.Formatter.format_email_address(record, %{type: type})
end

@doc false
Expand Down
1 change: 1 addition & 0 deletions test/lib/bamboo/adapters/test_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ defmodule Bamboo.TestAdapterTest do
end

sent_email |> TestMailer.deliver_now()
assert_email_delivered_with(from: "foo@bar.com")
Copy link
Contributor

Choose a reason for hiding this comment

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

It took me a second to understand this test, and I was wondering about the idea of having it be its own (named) test, but this does follow the existing pattern of including all of these within the same test about the helpers, so maybe making changes to it would be a separate issue? Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not a huge fan of this style, but I wasn't going to fight against the current standard. I don't have a strong opinion in favor of changing it but I would say it's a separate issue.

assert_email_delivered_with(from: {nil, "foo@bar.com"})

sent_email |> TestMailer.deliver_now()
Expand Down