gpt4 book ai didi

testing - Phoenix API : How do I test a POST/create when I don't know the ID of the new resource?

转载 作者:行者123 更新时间:2023-11-28 20:05:42 25 4
gpt4 key购买 nike

我有一个用于创建用户的 POST/api/v1/users 端点。这是我失败的测试:

test "success" do
user_params = %{
user: %{
email: "eric@spaghetti.com",
image_url: "https://www.google.com/images/spaghetti",
}
}

conn = build_conn(:post, "/api/v1/users", user_params)
response = Router.call(conn, @opts)
assert response.status == 201
assert Repo.get_by(User, %{email: user_params.user.email})
assert response.resp_body == %{
id: "how would I know this??", #<---------this would fail since I have no way of knowing what this value would be
email: user.email,
image_url: user.image_url,
} |> Poison.encode!
end

我的端点返回 3 个字段:email、image_url 和 id。我目前无法让这个测试通过,因为我无法知道新创建的资源的 ID 是什么。有没有更好的方法来测试这个?

最佳答案

在这种情况下,我通常会跳过检查确切的值 id,而只检查它是否存在:

%{email: email, image_url: image_url} = user

assert %{
"id" => _,
"email" => ^email,
"image_url" => ^image_url,
} = Poison.decode!(response.resp_body)

你也可以断言 id 是一个整数:

assert %{
"id" => id,
"email" => ^email,
"image_url" => ^image_url,
} = Poison.decode!(response.resp_body)
assert is_integer(id)

关于testing - Phoenix API : How do I test a POST/create when I don't know the ID of the new resource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43493716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com