Skip to content

Instantly share code, notes, and snippets.

View lasseebert's full-sized avatar

Lasse Skindstad Ebert lasseebert

View GitHub Profile
body = conn |> response(201) |> Poison.decode!
assert body["id"] |> is_integer
assert body["id"] > 0
assert body["email"] == "alice@example.com"
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
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
$ 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)
defmodule MyApp.Users.Invite do
alias MyApp.User
alias MyApp.Repo
@create_params [:email]
def call(%{email: email}) do
email
|> build_changeset
$ 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
defmodule MyApp.User do
use MyApp.Web, :model
schema "users" do
field :email, :string
timestamps
end
end
$ 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
$ 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
defmodule MyApp.Repo.Migrations.AddUsersTable do
use Ecto.Migration
def change do
create table(:users) do
add :email, :string
timestamps
end