I made a test to update the user changeset that should pass with invalid data, but the validation of the user aren't allowing it to pass , sorry if this is noob, im just starting at elixir
test "renders errors when data is invalid", %{conn: conn, user: user} do
assert conn = put(conn, Routes.api_user_path(conn, :update, user), user: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
The output
** (Ecto.InvalidChangesetError) could not perform update because changeset is invalid.
Errors
%{
email: [{"can't be blank", [validation: :required]}],
name: [{"can't be blank", [validation: :required]}],
password: [{"can't be blank", [validation: :required]}],
password__confirmation: [{"can't be blank", [validation: :required]}]
}
Applied changes
%{is_active: nil}
Params
%{
"email" => nil,
"is_active" => nil,
"name" => nil,
"password" => nil,
"password__confirmation" => nil
}
Changeset
Thats all lines been used
#Ecto.Changeset<
action: :update,
changes: %{is_active: nil},
errors: [
name: {"can't be blank", [validation: :required]},
email: {"can't be blank", [validation: :required]},
password: {"can't be blank", [validation: :required]},
password__confirmation: {"can't be blank", [validation: :required]}
],
data: #TrelloClone.Auth.User<>,
valid?: false
@invalid_attrs %{name: nil, email: nil, is_active: nil,password: nil,password__confirmation: nil}
def update(conn, %{"id" => id, "user" => user_params}) do
user = Auth.get_user!(id)
with {:ok, %User{} = user} <- Auth.update_user(user, user_params) do
render(conn, "show.json", user: user)
end
end
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update!()
end
def get_user!(id), do: Repo.get!(User, id)
Thats all lines been used, obs: they are in different files, Auth is the context of User
update
notupdate!
in yourupdate_user
function. – zwippie@invalid_attrs
map uses atom keys (e.g.%{id: nil}
, but yourupdate
function requires pattern-matching on string keys, e.g.%{"id" => id}
. Is that part of what's going on here? Can you paste in the changeset functions from your User module? – Everett