swoosh / swoosh

Compose, deliver and test your emails easily in Elixir
https://hexdocs.pm/swoosh
MIT License
1.43k stars 210 forks source link

`refute_email_sent/1` raises an exception but `assert_email_sent/1` does not with pinned emails #899

Closed PJUllrich closed 1 month ago

PJUllrich commented 2 months ago

Describe the bug

I try to assert that an email was sent to a user's email address, but refute that another email was sent as well.

My code looks something like this:

      assert_email_sent(
        subject: "Some subject",
        to: user.email
      )

      refute_email_sent(
        subject: "Another subject",
        to: user.email
      )

When I run the test, I receive an error like this:

** (ArgumentError) Unexpected tuple format, {{:., [line: 117], [{:user, [line: 117], nil}, :email]}, [no_parens: true, line: 117], []} cannot be formatted into a Recipient.

The expected format is {name :: String.t() | nil, address :: String.t()}, where address cannot be empty.

    (swoosh 1.11.5) lib/swoosh/email/recipient.ex:101: Swoosh.Email.Recipient.Tuple.format/1
    (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (swoosh 1.11.5) lib/swoosh/test_assertions.ex:246: Swoosh.TestAssertions.email_pattern/1
    (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (swoosh 1.11.5) expanding macro: Swoosh.TestAssertions.refute_email_sent/1

The problem seems to be that I cannot give a variable user.email as to:-option to the refute_email_sent/1 function, but I can it works just fine for the assert_email_sent/1 function.

I also tried pinning the ^user.email and to assign the email to a temporary variable like ^user_email, but neither worked.

Steps to Reproduce the Bug or Issue

Create a test like the above and run it.

Expected behavior

I'd expect both function assert_email_sent/1 and refute_email_sent/1 to accept the same attributes.

Your reproducible example

No response

Screenshots or Videos

No response

Platform

elixir 1.15.7-otp-26 erlang 26.1.2 swoosh 1.11.5

Additional context

No response

princemaple commented 2 months ago

https://github.com/swoosh/swoosh/issues/797#issuecomment-1662993654 Try the experimental one?

Or Jose's simpler one https://github.com/swoosh/swoosh/issues/488#issuecomment-1671224765

PJUllrich commented 1 month ago

@princemaple I can confirm that the experimental version of refute_email_sent works!

So, now I can do:

import Swoosh.X.TestAssertions

test "foo", %{user: user} do
  refute_email_sent(to: user.email, subject: "Some email")
end
princemaple commented 1 month ago

Thanks. Probably time for me to open an issue to settle on the new version.