在Phoenix应用程序中,我有一个控制器,该控制器的delete
函数实际上并未从数据库中删除该项目,而是对其进行了归档,并插入了一个与该项目的日期时间相对应的:utc_datetime
值。 “已删除”。这是函数:
def delete(conn, %{"id" => contact_id}) do
contact = Contact.get(contact_id)
case Contact.archive(contact) do
{:ok, _contact} ->
conn
|> put_flash(:info, "Contact successfully deleted.")
|> redirect(to: contact_path(conn, :index))
{:error, _changeset} ->
conn
|> put_flash(:error, "Contact deletion failed.")
|> redirect(to: contact_path(conn, :index))
end
end
这是Contact.archive
:
def archive(contact) do
contact
|> User.changeset(%{archived_at: DateTime.utc_now()})
|> Repo.update()
end
我的测试失败,并显示以下错误:(CaseClauseError) no case clause matching: {{{2018, 9, 6}, {6, 39, 5, 659053}}}
。这就是我为delete
设置测试的方式:
client =
:provider_user
|> build(
provider: conn.assigns.provider,
type: "external",
billable: true,
first_name: "client",
last_name: "user",
email: "client@user.com"
)
|> set_password("password")
|> insert
describe "delete contact" do
test "deletes contact with valid params", %{conn: conn, client: client} do
conn = delete(conn, contact_path(conn, :delete, client))
assert redirected_to(conn) == contact_path(conn, :index)
assert Repo.get(User, client.id).archived_at
end
end
如果有任何相关性,请参见Contact
的getter函数:
def get(contact_id, preload \\ []) do
query =
from(
u in User,
where: is_nil(u.archived_at) and u.id == ^contact_id,
preload: ^preload
)
Repo.one!(query)
end