Skip to content

Instantly share code, notes, and snippets.

View lasseebert's full-sized avatar

Lasse Skindstad Ebert lasseebert

View GitHub Profile
defmodule MyApp.UserController do
use MyApp.Web, :controller
def create(conn, params) do
attrs = %{email: params["email"]}
{:ok, user} = MyApp.Users.Invite.call(attrs)
{:ok, body} = %{
id: user.id,
email: user.email
$ mix test
Compiling 1 file (.ex)
warning: variable user is unused
web/controllers/user_controller.ex:6
....
1) test inviting a user responds with the new user (MyApp.UserController.InviteTest)
test/controllers/user_controller/invite_test.exs:4
Expected truthy, got false
$ mix test
...
1) test inviting a user responds with the new user (MyApp.UserController.InviteTest)
test/controllers/user_controller/invite_test.exs:4
** (FunctionClauseError) no function clause matching in MyApp.Users.Invite.call/1
stacktrace:
(my_app) lib/my_app/contexts/users/invite.ex:8: MyApp.Users.Invite.call(%{"email" => "alice@example.com"})
(my_app) web/controllers/user_controller.ex:5: MyApp.UserController.create/2
(my_app) web/controllers/user_controller.ex:1: MyApp.UserController.action/2
$ mix test test/contexts
.
Finished in 0.05 seconds
1 test, 0 failures
Randomized with seed 163288
defmodule MyApp.Repo.Migrations.AddUsersTable do
use Ecto.Migration
def change do
create table(:users) do
add :email, :string
timestamps
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.User do
use MyApp.Web, :model
schema "users" do
field :email, :string
timestamps
end
end
$ 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.Users.Invite do
alias MyApp.User
alias MyApp.Repo
@create_params [:email]
def call(%{email: email}) do
email
|> build_changeset