-
Notifications
You must be signed in to change notification settings - Fork 209
/
test.ex
49 lines (38 loc) · 1.11 KB
/
test.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
defmodule Swoosh.Adapters.Test do
@moduledoc ~S"""
An adapter that sends emails as messages to the current process.
This is meant to be used during tests and works with the assertions found in
the [Swoosh.TestAssertions](Swoosh.TestAssertions.html) module.
## Example
# config/test.exs
config :sample, Sample.Mailer,
adapter: Swoosh.Adapters.Test
# lib/sample/mailer.ex
defmodule Sample.Mailer do
use Swoosh.Mailer, otp_app: :sample
end
"""
use Swoosh.Adapter
def deliver(email, _config) do
for pid <- pids() do
send(pid, {:email, email})
end
{:ok, %{}}
end
def deliver_many(emails, _config) do
for pid <- pids() do
send(pid, {:emails, emails})
end
responses = for _email <- emails, do: %{}
{:ok, responses}
end
# Essentially finds all of the processes that tried to send an email (in the test)
# and sends an email to that process.
defp pids do
if pid = Application.get_env(:swoosh, :shared_test_process) do
[pid]
else
Enum.uniq([self() | List.wrap(Process.get(:"$callers"))])
end
end
end