3

Cannot figure out this error.

I have this file:

test/support/conn_case.ex

defmodule ProjectWeb.ConnCase do
  @moduledoc """
  This module defines the test case to be used by
  tests that require setting up a connection.

  Such tests rely on `Phoenix.ConnTest` and also
  import other functionality to make it easier
  to build common datastructures and query the data layer.

  Finally, if the test case interacts with the database,
  it cannot be async. For this reason, every test runs
  inside a transaction which is reset at the beginning
  of the test unless the test case is marked as async.
  """

  use ExUnit.CaseTemplate

  using do
    quote do
      # Import conveniences for testing with connections
      use Phoenix.ConnTest
      import ProjectWeb.Router.Helpers

      # The default endpoint for testing
      @endpoint ProjectWeb.Endpoint
    end
  end

end

And this config on mix.ex

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_),     do: ["lib"]

I have a test on test/controllers/page_controller_test.exs

defmodule ProjectWeb.PageControllerTest do
  use ProjectWeb.ConnCase

  test "GET /", %{conn: conn} do
    conn = get conn, "/"
    assert html_response(conn, 200) =~ "OK"
  end
end

Still when running mix test I receive:

** (CompileError) test/controllers/page_controller_test.exs:2: module ProjectWeb.ConnCase is not loaded and could not be found

lapinkoira
  • 8,320
  • 9
  • 51
  • 94

1 Answers1

6

EDIT: I left below the non-ideal solution just for the discussion of how you can edit the path, and for a lazy, mostly workable solution. Idiomatically, however, it would seem the practice is to manually set the env, rather than bork the dev ENV to get it to run your tests properly. You should just use MIX_ENV=test mix test to achieve your goals, as modifying the dev environment isn't desirable in most situations.

For anyone else coming across this, you may have had the same issue I had -> MIX_ENV is by default set to dev. If this is the case, you can easily test it by running MIX_ENV=test mix test, this will set the environment for the one call. If it works you have a workaround, and a more permanent one that I've described below.

BELOW THIS LINE REALLY ISN'T RECOMMENDED - Left for the discussion about how these modules are loaded and for those who don't care if the dev builds are clouded with test modules

The current way I've fixed it is to modify the mix.exs to look something like this:

defmodule MyApp.MixProject do
use Mix.Project

  def project do
    [
      ...
      elixirc_paths: elixirc_paths(Mix.env()),
      ...
    ]
  end

  # Configuration for the OTP application.
  #
  # Type `mix help compile.app` for more information.
  def application do
    [
      mod: {MyApp.Application, []},
      extra_applications: [:logger, :runtime_tools]
    ]
  end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(:dev), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

  # Specifies your project dependencies.
  #
  # Type `mix help deps` for examples and options.
  defp deps do
    [
      ...
    ]
  end
end

The important bit here that is different from the default mix.exs is the definition of elixirrc_paths in the def project block(this should already match, but if it doesn't, it should), and the addition of the line defp elixirc_paths(:dev), do: ["lib", "test/support"]

This may not be entirely idiomatic, but when using the dev mix environment, it will ensure your tests are compiled as well, and still allows you to separately define the dev and test environments.

Dwight
  • 360
  • 1
  • 3
  • 15