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
| body = conn |> response(201) |> Poison.decode! | |
| assert body["id"] |> is_integer | |
| assert body["id"] > 0 | |
| assert body["email"] == "alice@example.com" |
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 | |
| {:ok, user} = MyApp.Users.Invite.call(params) | |
| conn | |
| |> resp(201, "{}") | |
| 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 MyApp.Users.InviteTest do | |
| use MyApp.ModelCase | |
| alias MyApp.Users.Invite | |
| alias MyApp.User | |
| test "it creates a user" do | |
| {:ok, user} = Invite.call(%{email: "alice@example.com"}) | |
| assert user.id > 0 |
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 test/contexts | |
| 1) test it creates a user (MyApp.Users.InviteTest) | |
| test/contexts/users/invite_test.exs:6 | |
| ** (UndefinedFunctionError) function MyApp.Users.Invite.call/1 is undefined (module MyApp.Users.Invite is not available) | |
| stacktrace: | |
| MyApp.Users.Invite.call(%{email: "alice@example.com"}) | |
| test/contexts/users/invite_test.exs:7: (test) |
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.Users.Invite do | |
| alias MyApp.User | |
| alias MyApp.Repo | |
| @create_params [:email] | |
| def call(%{email: email}) do | |
| |> build_changeset |
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 test/contexts | |
| Compiling 1 file (.ex) | |
| == Compilation error on file lib/my_app/contexts/users/invite.ex == | |
| ** (CompileError) lib/my_app/contexts/users/invite.ex:17: MyApp.User.__struct__/1 is undefined, cannot expand struct MyApp.User | |
| (stdlib) lists.erl:1354: :lists.mapfoldl/3 | |
| (stdlib) lists.erl:1354: :lists.mapfoldl/3 |
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.User do | |
| use MyApp.Web, :model | |
| schema "users" do | |
| field :email, :string | |
| timestamps | |
| 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
| $ mix test test/contexts | |
| 1) test it creates a user (MyApp.Users.InviteTest) | |
| test/contexts/users/invite_test.exs:7 | |
| ** (Postgrex.Error) ERROR (undefined_table): relation "users" does not exist | |
| stacktrace: | |
| (ecto) lib/ecto/adapters/sql.ex:463: Ecto.Adapters.SQL.struct/6 | |
| (ecto) lib/ecto/repo/schema.ex:397: Ecto.Repo.Schema.apply/4 | |
| (ecto) lib/ecto/repo/schema.ex:193: anonymous fn/11 in Ecto.Repo.Schema.do_insert/4 |
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 test/contexts | |
| 1) test it creates a user (MyApp.Users.InviteTest) | |
| test/contexts/users/invite_test.exs:7 | |
| ** (Postgrex.Error) ERROR (undefined_table): relation "users" does not exist | |
| stacktrace: | |
| (ecto) lib/ecto/adapters/sql.ex:463: Ecto.Adapters.SQL.struct/6 | |
| (ecto) lib/ecto/repo/schema.ex:397: Ecto.Repo.Schema.apply/4 | |
| (ecto) lib/ecto/repo/schema.ex:193: anonymous fn/11 in Ecto.Repo.Schema.do_insert/4 |
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.Repo.Migrations.AddUsersTable do | |
| use Ecto.Migration | |
| def change do | |
| create table(:users) do | |
| add :email, :string | |
| timestamps | |
| end |