Improve tests: better assertions, meaningful tests
Track resulting principles in project conventions
This commit is contained in:
@@ -12,15 +12,23 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
|
||||
|> Keyword.fetch!(:api_token)
|
||||
end
|
||||
|
||||
describe "authentication" do
|
||||
test "all API endpoints require a bearer token", %{conn: conn} do
|
||||
for path <- [
|
||||
~p"/api/collection/latest",
|
||||
~p"/api/collection/random",
|
||||
~p"/api/collection",
|
||||
~p"/api/collection/on_this_day"
|
||||
] do
|
||||
assert get(conn, path).status == 401,
|
||||
"expected 401 for unauthenticated GET #{path}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /api/collection/latest" do
|
||||
setup [:create_record]
|
||||
|
||||
test "requires authentication", %{conn: conn} do
|
||||
conn = get(conn, ~p"/api/collection/latest")
|
||||
|
||||
assert conn.status == 401
|
||||
end
|
||||
|
||||
test "returns the latest record", %{conn: conn, record: record} do
|
||||
conn =
|
||||
conn
|
||||
@@ -34,12 +42,6 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
|
||||
describe "GET /api/collection/random" do
|
||||
setup [:create_record]
|
||||
|
||||
test "requires authentication", %{conn: conn} do
|
||||
conn = get(conn, ~p"/api/collection/random")
|
||||
|
||||
assert conn.status == 401
|
||||
end
|
||||
|
||||
# We're not testing random here - the query is solid enough
|
||||
test "returns a random record", %{conn: conn, record: record} do
|
||||
conn =
|
||||
@@ -54,12 +56,6 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
|
||||
describe "GET /api/collection" do
|
||||
setup [:create_record]
|
||||
|
||||
test "requires authentication", %{conn: conn} do
|
||||
conn = get(conn, ~p"/api/collection")
|
||||
|
||||
assert conn.status == 401
|
||||
end
|
||||
|
||||
test "returns a paginated list of records", %{conn: conn, record: record} do
|
||||
conn =
|
||||
conn
|
||||
@@ -78,12 +74,6 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
|
||||
describe "GET /api/collection/on_this_day" do
|
||||
setup [:create_record]
|
||||
|
||||
test "requires authentication", %{conn: conn} do
|
||||
conn = get(conn, ~p"/api/collection/on_this_day")
|
||||
|
||||
assert conn.status == 401
|
||||
end
|
||||
|
||||
test "returns a list of records", %{conn: conn, record: record} do
|
||||
conn =
|
||||
conn
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
defmodule MusicLibraryWeb.ErrorHTMLTest do
|
||||
use MusicLibraryWeb.ConnCase, async: true
|
||||
|
||||
# Bring render_to_string/4 for testing custom views
|
||||
import Phoenix.Template
|
||||
|
||||
test "renders 404.html" do
|
||||
assert render_to_string(MusicLibraryWeb.ErrorHTML, "404", "html", []) == "Not Found"
|
||||
end
|
||||
|
||||
test "renders 500.html" do
|
||||
assert render_to_string(MusicLibraryWeb.ErrorHTML, "500", "html", []) ==
|
||||
"Internal Server Error"
|
||||
end
|
||||
end
|
||||
@@ -1,12 +0,0 @@
|
||||
defmodule MusicLibraryWeb.ErrorJSONTest do
|
||||
use MusicLibraryWeb.ConnCase, async: true
|
||||
|
||||
test "renders 404" do
|
||||
assert MusicLibraryWeb.ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}}
|
||||
end
|
||||
|
||||
test "renders 500" do
|
||||
assert MusicLibraryWeb.ErrorJSON.render("500.json", %{}) ==
|
||||
%{errors: %{detail: "Internal Server Error"}}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user