Я провел тест для обновления набора изменений пользователя, который должен пройти с недопустимыми данными, но проверка пользователя не позволяет ему пройти, извините, если это нуб, я просто начинаю с 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
Вывод
** (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)
Это все строки были использованы, obs: они находятся в разных файлах, Auth - это контекст пользователя