I generated a live resource using a mix task mix phx.gen.live
. Compared to Rails, no routes were added to the router.ex
file. So I had to add some manually like this:
scope "/", XClarityWeb do
pipe_through :browser
get "/", PageController, :home
live "/clients", ClientLive.Index, :index
live "/clients/new", ClientLive.Index, :new
live "/clients/:id/edit", ClientLive.Index, :edit
end
...
When starting the app and playing around with it, - everything seems to work as needed (CRUD work fine).
But when running the tests, I have 2 of them failing saying:
1) test Index saves new client (XClarityWeb.ClientLiveTest)
test/xclarity_web/live/client_live_test.exs:26
** (ArgumentError) expected XClarityWeb.ClientLive.Index to patch to "/clients", but got none
code: assert_patch(index_live, ~p"/clients")
stacktrace:
(phoenix_live_view 0.18.18) lib/phoenix_live_view/test/live_view_test.ex:1314: Phoenix.LiveViewTest.assert_navigation/4
(phoenix_live_view 0.18.18) lib/phoenix_live_view/test/live_view_test.ex:1214: Phoenix.LiveViewTest.assert_patch/3
test/xclarity_web/live/client_live_test.exs:42: (test)
2) test Index updates client in listing (XClarityWeb.ClientLiveTest)
test/xclarity_web/live/client_live_test.exs:49
** (ArgumentError) expected XClarityWeb.ClientLive.Index to patch to "/clients", but got none
code: assert_patch(index_live, ~p"/clients")
stacktrace:
(phoenix_live_view 0.18.18) lib/phoenix_live_view/test/live_view_test.ex:1314: Phoenix.LiveViewTest.assert_navigation/4
(phoenix_live_view 0.18.18) lib/phoenix_live_view/test/live_view_test.ex:1214: Phoenix.LiveViewTest.assert_patch/3
test/xclarity_web/live/client_live_test.exs:65: (test)
The content of the Index
module is as follows:
defmodule XClarityWeb.ClientLive.Index do
use XClarityWeb, :live_view
alias XClarity.Clients
alias XClarity.Clients.Client
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :clients, Clients.list_clients())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Client")
|> assign(:client, Clients.get_client!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Client")
|> assign(:client, %Client{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Clients")
|> assign(:client, nil)
end
@impl true
def handle_info({XClarityWeb.ClientLive.FormComponent, {:saved, client}}, socket) do
{:noreply, stream_insert(socket, :clients, client)}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
client = Clients.get_client!(id)
{:ok, _} = Clients.delete_client(client)
{:noreply, stream_delete(socket, :clients, client)}
end
end
I can’t figure out:
- what the reason of the failure is
- should I declare manually PATCH, POST and DELETE routes?
{:phoenix, "~> 1.7.2"},
{:phoenix_html, "~> 3.3"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.18.16"},
...
Thank you.