I’m adding some tests to my project with LiveViewTest and I’m inserting some data into the database that isn’t available when the LiveView is tested when using async: true.
I didn’t find anything in the documentation but it isn’t possible to run these tests asynchronously? Or I’m missing anything?
defmodule MyAppWeb.CrawlerLiveTest do
use MyAppWeb.ConnCase
import Phoenix.LiveViewTest
describe "Index" do
test "Connected mount", %{conn: conn} do
user = insert(:user)
{:ok, _view, html} =
conn
|> Plug.Test.init_test_session(user_id: user.id)
|> live("/crawler")
assert html =~ "Images of Crawler"
end
end
end
conn_case.ex
...
setup tags do
MyApp.DataCase.setup_sandbox(tags)
%{conn: Phoenix.ConnTest.build_conn()}
end
data_case.ex
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(@repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end
Inside the live view I check all the users available in the database and the new users isn’t there, so the test fails with async: true. Without this, it pass.