legendary-doc-site/apps/app/priv/templates/phx.gen.html/controller_test.exs

90 lines
4 KiB
Elixir
Raw Normal View History

2020-07-29 17:07:36 +00:00
defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>ControllerTest do
use <%= inspect context.web_module %>.ConnCase
2021-04-23 21:52:57 +00:00
alias <%= inspect context.module %>
2020-07-29 17:07:36 +00:00
@create_attrs <%= inspect schema.params.create %>
@update_attrs <%= inspect schema.params.update %>
@invalid_attrs <%= inspect for {key, _} <- schema.params.create, into: %{}, do: {key, nil} %>
2021-04-23 21:52:57 +00:00
def fixture(:<%= schema.singular %>) do
{:ok, <%= schema.singular %>} = <%= inspect context.alias %>.create_<%= schema.singular %>(@create_attrs)
<%= schema.singular %>
end
2020-07-29 17:07:36 +00:00
describe "index" do
test "lists all <%= schema.plural %>", %{conn: conn} do
conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :index))
2021-04-23 21:52:57 +00:00
assert html_response(conn, 200) =~ "<%= schema.human_plural %>"
2020-07-29 17:07:36 +00:00
end
end
describe "new <%= schema.singular %>" do
test "renders form", %{conn: conn} do
conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :new))
2021-04-23 21:52:57 +00:00
assert html_response(conn, 200) =~ "Save"
2020-07-29 17:07:36 +00:00
end
end
describe "create <%= schema.singular %>" do
test "redirects to show when data is valid", %{conn: conn} do
2021-04-23 21:52:57 +00:00
post_conn = post(conn, Routes.<%= schema.route_helper %>_path(conn, :create), <%= schema.singular %>: @create_attrs)
2020-07-29 17:07:36 +00:00
2021-04-23 21:52:57 +00:00
assert %{id: id} = redirected_params(post_conn)
assert redirected_to(post_conn) == Routes.<%= schema.route_helper %>_path(post_conn, :show, id)
2020-07-29 17:07:36 +00:00
2021-04-23 21:52:57 +00:00
get_conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, id))
assert html_response(get_conn, 200) =~ "Edit"
2020-07-29 17:07:36 +00:00
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.<%= schema.route_helper %>_path(conn, :create), <%= schema.singular %>: @invalid_attrs)
2021-04-23 21:52:57 +00:00
assert html_response(conn, 200) =~ "Save"
2020-07-29 17:07:36 +00:00
end
end
describe "edit <%= schema.singular %>" do
setup [:create_<%= schema.singular %>]
test "renders form for editing chosen <%= schema.singular %>", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :edit, <%= schema.singular %>))
assert html_response(conn, 200) =~ "Edit <%= schema.human_singular %>"
end
end
describe "update <%= schema.singular %>" do
setup [:create_<%= schema.singular %>]
test "redirects when data is valid", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
2021-04-23 21:52:57 +00:00
put_conn = put(conn, Routes.<%= schema.route_helper %>_path(conn, :update, <%= schema.singular %>), <%= schema.singular %>: @update_attrs)
assert redirected_to(put_conn) == Routes.<%= schema.route_helper %>_path(put_conn, :show, <%= schema.singular %>)
2020-07-29 17:07:36 +00:00
2021-04-23 21:52:57 +00:00
get_conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))<%= if schema.string_attr do %>
assert html_response(get_conn, 200) =~ <%= inspect Mix.Phoenix.Schema.default_param(schema, :update) %><% else %>
assert html_response(get_conn, 200)<% end %>
2020-07-29 17:07:36 +00:00
end
test "renders errors when data is invalid", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
conn = put(conn, Routes.<%= schema.route_helper %>_path(conn, :update, <%= schema.singular %>), <%= schema.singular %>: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit <%= schema.human_singular %>"
end
end
describe "delete <%= schema.singular %>" do
setup [:create_<%= schema.singular %>]
test "deletes chosen <%= schema.singular %>", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
2021-04-23 21:52:57 +00:00
delete_conn = delete(conn, Routes.<%= schema.route_helper %>_path(conn, :delete, <%= schema.singular %>))
assert redirected_to(delete_conn) == Routes.<%= schema.route_helper %>_path(delete_conn, :index)
2020-07-29 17:07:36 +00:00
assert_error_sent 404, fn ->
get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))
end
end
end
defp create_<%= schema.singular %>(_) do
2021-04-23 21:52:57 +00:00
<%= schema.singular %> = fixture(:<%= schema.singular %>)
2020-07-29 17:07:36 +00:00
%{<%= schema.singular %>: <%= schema.singular %>}
end
end