How do I test my LiveView

I have a Phoenix LiveView app that I want to write tests for.

The app is a game and the state of the game is stored in a struct on socket assigns.

With it being a game it’s difficult to replicate the user action to get the page in the state that I want to write assertions on.

Instead I planned on setting the game struct on socket assigns in the test and then asserting that the page is as expected. My test looks something like this:

test "it displays when the game is won", %{conn: conn} do
  conn = assign(conn, :game, %Game{state: :won})
  {:ok, view, html} = live(conn, "/")
  assert render(view) =~ "Won!"

When I inspect the socket within the mount method of my LiveView module it doesn’t seem to have the values that I have setup in my test. For reference, the mount method creates a new game and assigns it to the socket.

  def mount(_params, _session, socket) do
    {:ok, new_game(socket)}

Is what I’m trying to do incorrect? Should I be writing assertions for just the view instead, does the mount method need to change so I can pass in different game states (some form of dependency injection), or is there a better way of doing writing these tests