Skip to content

Instantly share code, notes, and snippets.

View lasseebert's full-sized avatar

Lasse Skindstad Ebert lasseebert

View GitHub Profile
defmodule Hub do
@moduledoc """
Publish/subscribe server.
Subscription is done with a pattern.
Example:
{:ok, _pid} = Hub.start_link(name: :hub)
Hub.subscribe(:hub, %{count: count} when count > 42)

I hereby claim:

  • I am lasseebert on github.
  • I am lasseebert (https://keybase.io/lasseebert) on keybase.
  • I have a public key ASCwCSpB0kgQzneAi5asC6XLsoCFuUaiQa4wQICeeZkKqQo

To claim this, I am signing this object:

$ mix test
........
Finished in 0.09 seconds
8 tests, 0 failures
Randomized with seed 776617
$ mix test
.......
1) test it uses mock in tests (MyApp.MailerTest)
test/mailer_test.exs:4
** (exit) exited in: GenServer.call(MyApp.Mailer.Mock, {:update, #Function<0.6592501/1 in MyApp.Mailer.Mock.clear/0>}, 5000)
** (EXIT) no process
stacktrace:
(elixir) lib/gen_server.ex:596: GenServer.call/3
test/mailer_test.exs:5: (test)
$ 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
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)
defmodule MyApp.Mailer do
@from "[email protected]"
@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(
$ 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.MailerTest do
use ExUnit.Case
test "it uses mock in tests" do
MyApp.Mailer.Mock.clear
MyApp.Mailer.send_test_mail("[email protected]")
assert MyApp.Mailer.Mock.mails |> length == 1
[mail] = MyApp.Mailer.Mock.mails
defmodule MyApp.Mailer do
use Mailgun.Client, [
domain: Application.fetch_env!(:my_app, :mailgun_domain),
key: Application.fetch_env!(:my_app, :mailgun_key)
]
@from "[email protected]"
def send_test_mail(email) do
{:ok, _} = send_email [