为什么这个测试没有通过?
Why isnt this test passing?
我做了一个测试来更新应该通过无效数据的用户变更集,但是用户的验证不允许它通过,对不起,如果这是菜鸟,我刚从长生不老药开始
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 是用户的上下文
根据文档,Repo.update!
您在 update_user/2
中使用
Same as Repo.update/2
but returns the struct or raises if the changeset is invalid.
在无效的变更集上引发了异常,您的测试失败了。如果你确实想 raise 错误的输入,你可以用 ExUnit.Assertions.assert_raise/2
修改测试
test "renders errors when data is invalid", %{conn: conn, user: user} do
assert_raise Ecto.InvalidChangesetError, fn ->
put(conn, Routes.api_user_path(conn, :update, user), user: @invalid_attrs)
end
end
但是既然要显示422
,最好在update_user/2
中使用Repo.update/2
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update() # not Repo.update!()
end
我做了一个测试来更新应该通过无效数据的用户变更集,但是用户的验证不允许它通过,对不起,如果这是菜鸟,我刚从长生不老药开始
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 是用户的上下文
根据文档,Repo.update!
您在 update_user/2
Same as
Repo.update/2
but returns the struct or raises if the changeset is invalid.
在无效的变更集上引发了异常,您的测试失败了。如果你确实想 raise 错误的输入,你可以用 ExUnit.Assertions.assert_raise/2
test "renders errors when data is invalid", %{conn: conn, user: user} do
assert_raise Ecto.InvalidChangesetError, fn ->
put(conn, Routes.api_user_path(conn, :update, user), user: @invalid_attrs)
end
end
但是既然要显示422
,最好在update_user/2
Repo.update/2
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update() # not Repo.update!()
end