I tried to add a callback (authenticate/1) to authenticate a user before every test as follows:
defmodule XClarityWeb.ClientLiveTest do
use XClarityWeb.ConnCase
import Phoenix.LiveViewTest
import XClarity.ClientsFixtures
import XClarity.AccountsFixtures
defp create_client(_) do
client = client_fixture()
%{client: client}
end
defp authenticate(conn) do
conn
|> login_user(user_fixture())
end
describe "Index" do
setup [:create_client]
setup %{conn: conn} do
authenticate(conn)
end
test "lists all clients", %{conn: conn, client: client} do
{:ok, _index_live, html} = live(conn, ~p"/clients")
assert html =~ "Listing Clients"
assert html =~ client.name
end
...
but it failed with:
error: undefined function login_user/2 (expected XClarityWeb.ClientLiveTest to define such a function or for it to be imported, but none are available)
test/xclarity_web/live/client_live_test.exs:19: XClarityWeb.ClientLiveTest.authenticate/1
But if I call login_user direcctly in a test example like this:
describe "Index" do
setup [:create_client]
# setup %{conn: conn} do
# authenticate(conn)
# end
test "lists all clients", %{conn: conn, client: client} do
{:ok, _lv, html} =
conn
|> log_in_user(user_fixture())
|> live(~p"/clients")
assert html =~ "Listing Clients"
assert html =~ client.name
end
it works :(.
Why so?