Current section

Files

Jump to
caravela priv templates rest_controller_test.eex
Raw

priv/templates/rest_controller_test.eex

defmodule <%= inspect @module %> do
@moduledoc """
Generated by Caravela from <%= inspect @domain_module %>.
Skeleton tests for `<%= inspect @controller_module %>`. One
`describe` per action (index / show / new / edit / create /
update / delete), one test per happy/unhappy path. Fill in
fixture setup where marked `# TODO:` and replace placeholder
assertions with the ones specific to your app's UI.
Regenerate with `mix caravela.gen.live <%= inspect @domain_module %>`.
Custom code placed below the `# --- CUSTOM ---` marker is preserved.
"""
use <%= inspect @conn_case %>, async: true
alias <%= inspect @context_module %>
# --- Fixtures --------------------------------------------------------
defp build_<%= @singular %>_fixture(attrs \\ %{}) do
{:ok, entity} =
attrs
|> Enum.into(%{})
|> <%= @context_short %>.<%= @create_fn %>(build_context())
entity
end
defp build_context, do: %{current_user: nil}
# --- index ----------------------------------------------------------
describe "GET <%= @index_path %>" do
test "returns 200 with the collection rendered", %{conn: conn} do
_entity = build_<%= @singular %>_fixture()
conn = get(conn, "<%= @index_path %>")
assert html_response(conn, 200)
# TODO: assert a field of the fixture appears in the response body.
end
end
# --- show -----------------------------------------------------------
describe "GET <%= @index_path %>/:id" do
test "returns 200 for an existing record", %{conn: conn} do
entity = build_<%= @singular %>_fixture()
conn = get(conn, "<%= @index_path %>/" <> to_string(entity.id))
assert html_response(conn, 200)
end
test "404s on a non-existent id", %{conn: conn} do
# TODO: pick an id shape that matches your primary key type.
conn = get(conn, "<%= @index_path %>/00000000-0000-0000-0000-000000000000")
assert response(conn, 404)
end
end
# --- new / edit (form renders) --------------------------------------
describe "GET <%= @index_path %>/new" do
test "renders the empty form", %{conn: conn} do
conn = get(conn, "<%= @index_path %>/new")
assert html_response(conn, 200)
end
end
describe "GET <%= @index_path %>/:id/edit" do
test "renders the prefilled form", %{conn: conn} do
entity = build_<%= @singular %>_fixture()
conn = get(conn, "<%= @index_path %>/" <> to_string(entity.id) <> "/edit")
assert html_response(conn, 200)
end
end
# --- create ----------------------------------------------------------
describe "POST <%= @index_path %>" do
test "redirects on success", %{conn: conn} do
# TODO: fill valid params for your entity.
params = %{"<%= @singular %>" => %{}}
conn = post(conn, "<%= @index_path %>", params)
assert redirected_to(conn) =~ "<%= @index_path %>"
end
test "re-renders the form with structured errors on failure", %{conn: conn} do
# TODO: send params missing a required field; assert the
# response carries a %{code: :required} entry under that
# field (see Caravela.ChangesetTranslator).
conn = post(conn, "<%= @index_path %>", %{"<%= @singular %>" => %{}})
assert html_response(conn, 422) || html_response(conn, 200)
end
end
# --- update ----------------------------------------------------------
describe "PATCH <%= @index_path %>/:id" do
test "redirects on success", %{conn: conn} do
entity = build_<%= @singular %>_fixture()
# TODO: fill update params for your entity.
params = %{"<%= @singular %>" => %{}}
conn = patch(conn, "<%= @index_path %>/" <> to_string(entity.id), params)
assert redirected_to(conn) =~ "<%= @index_path %>"
end
end
# --- delete ----------------------------------------------------------
describe "DELETE <%= @index_path %>/:id" do
test "removes the record and redirects", %{conn: conn} do
entity = build_<%= @singular %>_fixture()
conn = delete(conn, "<%= @index_path %>/" <> to_string(entity.id))
assert redirected_to(conn) == "<%= @index_path %>"
end
end
<%= @custom_marker %>
end