From 2ffaa597be71b5305da56561910f943ce97eaab5 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Mon, 28 Oct 2024 11:01:04 -0400 Subject: [PATCH] cleanup(ex/test): general cleanup related to upgrading to the latest Phoenix version and Template (#2868) * cleanup(ex/test_helper): combine `ensure_application_started` calls * doc:cleanup(ex/test_helper): add mocks section * chore:test(ex/data_case): update `errors_on` helper to `phx.new` template * cleanup:test(ex/conn_case): reorder 'using' macro acording to `phx.new` template --- test/support/conn_case.ex | 10 +++++----- test/support/data_case.ex | 6 +++--- test/test_helper.exs | 8 ++++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 24ed4fc1f..c52a382b5 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -21,15 +21,15 @@ defmodule SkateWeb.ConnCase do using do quote do - # Import conveniences for testing with connections - import Plug.Conn - import Phoenix.ConnTest - alias SkateWeb.Router.Helpers, as: Routes - # The default endpoint for testing @endpoint SkateWeb.Endpoint use SkateWeb, :verified_routes + + # Import conveniences for testing with connections + import Plug.Conn + import Phoenix.ConnTest + import SkateWeb.ConnCase end end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 053c35f28..b0b238c67 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -41,7 +41,7 @@ defmodule Skate.DataCase do end @doc """ - A helper that transform changeset errors to a map of messages. + A helper that transforms changeset errors into a map of messages. assert {:error, changeset} = Accounts.create_user(%{password: "short"}) assert "password is too short" in errors_on(changeset).password @@ -50,8 +50,8 @@ defmodule Skate.DataCase do """ def errors_on(changeset) do Ecto.Changeset.traverse_errors(changeset, fn {message, opts} -> - Enum.reduce(opts, message, fn {key, value}, acc -> - String.replace(acc, "%{#{key}}", to_string(value)) + Regex.replace(~r"%{(\w+)}", message, fn _, key -> + opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string() end) end) end diff --git a/test/test_helper.exs b/test/test_helper.exs index 56cb0a320..824788180 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,5 +1,8 @@ -Application.ensure_all_started(:stream_data) -{:ok, _} = Application.ensure_all_started(:ex_machina) +{:ok, _} = + Application.ensure_all_started([ + :stream_data, + :ex_machina + ]) ExUnit.start( capture_log: true, @@ -13,6 +16,7 @@ ExUnit.start( Ecto.Adapters.SQL.Sandbox.mode(Skate.Repo, :manual) +# Mocks Mox.defmock(Skate.OpenRouteServiceAPI.MockClient, for: Skate.OpenRouteServiceAPI.Client) Application.put_env(:skate, Skate.OpenRouteServiceAPI,