Skip to content

Instantly share code, notes, and snippets.

View lasseebert's full-sized avatar

Lasse Skindstad Ebert lasseebert

View GitHub Profile
class InviteUser
def initialize(mailer: Mailer.new, user_repo: UserRepo.new)
@mailer = mailer
@user_repo = user_repo
end
attr_reader :mailer
attr_reader :user_repo
def invite(email)
defmodule InviteUser do
def invite(email, mailer \\ Mailer) do
user = create(email)
mailer.send_activation_mail(user)
end
end
defmodule InviteUser do
def invite(email) do
user = create(email)
MyApp.Pact.get("mailer").send_activation_mail(user)
end
end
defmodule MyTest do
use ExUnit.Case, async: false
import Mock
test "multiple mocks" do
with_mocks([
{HashDict,
[],
[get: fn(%{}, "http://example.com") -> "<html></html>" end]},
defmodule MyApp.Mailer do
use Mailgun.Client, [
domain: Application.fetch_env!(:my_app, :mailgun_domain),
key: Application.fetch_env!(:my_app, :mailgun_key)
]
@from "support@myapp.com"
def send_test_mail(email) do
{:ok, _} = send_email [
defmodule MyApp.MailerTest do
use ExUnit.Case
test "it uses mock in tests" do
MyApp.Mailer.Mock.clear
MyApp.Mailer.send_test_mail("me@example.com")
assert MyApp.Mailer.Mock.mails |> length == 1
[mail] = MyApp.Mailer.Mock.mails
$ mix test
Compiling 16 files (.ex)
== Compilation error on file lib/my_app/mailer.ex ==
** (ArgumentError) application :my_app is not loaded, or the configuration parameter :mailgun_domain is not set
(elixir) lib/application.ex:261: Application.fetch_env!/2
lib/my_app/mailer.ex:3: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
defmodule MyApp.Mailer do
@from "support@myapp.com"
@mailer_impl Application.fetch_env!(:my_app, :mailer)
defmodule Behaviour do
@callback send_mail([key: String.t]) :: :ok
end
def send_test_mail(email) do
:ok = @mailer_impl.send_mail(
defmodule MyApp.Mailer.Mailgun do
@behaviour MyApp.Mailer.Behaviour
use Mailgun.Client, [
domain: Application.fetch_env!(:my_app, :mailgun_domain),
key: Application.fetch_env!(:my_app, :mailgun_key)
]
def send_mail(mail) do
{:ok, _} = send_email(mail)
$ mix test
Compiling 18 files (.ex)
== Compilation error on file lib/my_app/mailer/mailgun.ex ==
** (ArgumentError) application :my_app is not loaded, or the configuration parameter :mailgun_domain is not set
(elixir) lib/application.ex:261: Application.fetch_env!/2
lib/my_app/mailer/mailgun.ex:5: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6