How to write feature tests using Wallaby that relies on a GenServer?

I have gone through a few iterations of getting this test to work, and I’ve reach the point where my GenServer is started by the test and will have the same database connection as the test (the GenServer needs to access the database).

But it seems now that when my controller attempts to call the GenServer, it doesn’t look to be started, as I see this error

|| 08:30:30.194 [error] #PID<0.487.0> running AppWeb.Endpoint (cowboy_protocol) terminated
|| Server: localhost:4001 (http)
|| Request: GET /
|| ** (exit) exited in: GenServer.call(App.Core.AppServer, :status, 5000)
||     ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started

So with that said, does anyone have any general advice for writing Wallaby tests for endpoints that utilize a GenServer? I feel like I may have missed something along the way. (Help with this specific problem is also appreciated.)

Thanks everyone!

branch on github in case the snippets below are not sufficient


Controller that access the GenServer

defmodule AppWeb.ThingController do
  use AppWeb, :controller
  alias App.Core.AppServer

  def index(conn, _) do
    thing = AppServer.status(AppServer)

    conn
    |> render(:index, thing: thing)
  end
end

Wallaby test.

test "should display things on the home page", %{session: session} do
  thing_fixture(%{
    name: "name",
    url: "url"
  })

  start_supervised!(FeedServer)

  session
  |> visit("/")
  |> assert_text("Some text")
end

Note: I only don’t start the GenServer in the test environment, so I’m able to start it in the context of the test and will be supervised by the test supervisor.

defmodule App.Application do
  use Application

  def start(_type, _args) do
    children =
      [
        App.Repo,
        AppWeb.Endpoint
      ] ++ Application.get_env(:app, :app_server)

    opts = [strategy: :one_for_one, name: App.Supervisor]
    Supervisor.start_link(children, opts)
  end

  def config_change(changed, _new, removed) do
    AppWeb.Endpoint.config_change(changed, removed)
    :ok
  end
end

Another way to handle this might be to disable the async flag for this test case. If you do that then you can explicitly terminate your gen_server once your test is done. Thats the tactic that we employed at my last job anyway.

Was AppServer renamed to FeedServer?

Sorry, it should say AppServer, it appears I wasn’t thorough enough in the censoring of my snippets.

I have async disabled for these tests.

I believe that when you start a process with start_supervised it will get terminated at the end of the test.

I haven’t had a lot of time to work on this, but I’ll update the thread once I find a solution.

Thanks!

I have gotten to the bottom of this error, and I am quite embarrassed as to the cause.

In an attempt to clean up how my genserver was started, I accidentally removed the third argument to GenServer.start_link/3 which in turn did register the process with the module name.

Thanks for the help @keathley and @mbuhot.

1 Like