This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule InviteUser do | |
| def invite(email) do | |
| user = create(email) | |
| MyApp.Pact.get("mailer").send_activation_mail(user) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule InviteUser do | |
| def invite(email, mailer \\ Mailer) do | |
| user = create(email) | |
| mailer.send_activation_mail(user) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule MyApp.UserController do | |
| use MyApp.Web, :controller | |
| def create(conn, params) do | |
| attrs = %{email: params["email"]} | |
| case MyApp.Users.Invite.call(attrs) do | |
| {:ok, user} -> | |
| render_user(conn, user) | |
| {:error, :invalid_email} -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule MyApp.UserController do | |
| use MyApp.Web, :controller | |
| def create(conn, params) do | |
| attrs = %{email: params["email"]} | |
| case MyApp.Users.Invite.call(attrs) do | |
| {:ok, user} -> | |
| render_user(conn, user) | |
| {:error, :invalid_email} -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def call(%{email: email}) do | |
| with :ok <- validate_email(email) do | |
| |> build_changeset | |
| |> create_user | |
| end | |
| end | |
| #... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test "it handles invalid email" do | |
| {:error, :invalid_email} = Invite.call(%{email: "Alice and Bob"}) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test "invalid email gives a 400 response" do | |
| conn = | |
| build_conn | |
| |> post("/users", email: "Alice and Bob") | |
| body = conn |> response(400) |> Poison.decode! | |
| assert body["message"] == "email is invalid" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ mix test | |
| Compiling 1 file (.ex) | |
| ..... | |
| Finished in 0.08 seconds | |
| 5 tests, 0 failures | |
| Randomized with seed 174980 |