I’m a noob and am currently creating a little test project: a read-only API and CRUD admin pages. Somewhere along the line a controller test started failing, which I think is because I added attributes other than the ID of the model to the API JSON output.
The API controller test:
test "shows chosen resource", %{conn: conn} do
material = Repo.insert! %Material{}
conn = get conn, api_material_path(conn, :show, material)
assert json_response(conn, 200)["data"] == %{"id" => material.id}
end
The test failing:
1) test shows chosen resource (AppName.Api.MaterialControllerTest)
test/controllers/api/material_controller_test.exs:18
Assertion with == failed
code: json_response(conn, 200)["data"] == {}
lhs: %{"id" => 28, "name" => nil, "price_sq_m" => nil}
rhs: {}
stacktrace:
test/controllers/api/material_controller_test.exs:21: (test)
The relevant view function:
def render("material.json", %{material: material}) do
material
|> Map.take([:id, :price_sq_m, :name])
end
If I change the test’s assert
to compare against %{"id" => material.id, "name" => material.name, "price_sq_m" => material.price_sq_m}
it passes.
I’ve not worked with tests before. Is updating the test in this manner a reasonable means of getting it to pass, or is there a better/easier/more robust way? Thanks.