mercredi 19 avril 2017

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

I have an a POST /api/v1/users endpoint for creating a user. This is my failing test for it:

test "success" do
  user_params = %{
    user: %{
      email: "eric@spaghetti.com",
      image_url: "http://ift.tt/2o2USQs",
    }
  }

  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

My endpoint returns 3 fields: email, image_url, and id. I am currently unable to get this test to pass because there's no way I can tell what the ID of the newly created resource will be. Is there a better way to test this?

Aucun commentaire:

Enregistrer un commentaire