未加载模块ConnCase且找不到

时间:2018-08-10 14:03:59

标签: testing phoenix-framework mix

无法确定此错误。

我有这个文件:

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

mix.ex上的此配置

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

我在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

在运行mix test时仍然显示:

  

**(CompileError)test / controllers / page_controller_test.exs:2:未加载模块ProjectWeb.ConnCase且找不到

1 个答案:

答案 0 :(得分:0)

对于遇到此问题的任何其他人,您可能遇到的问题与我一样-> (2*3)!默认设置为dev。在这种情况下,您可以通过运行switch (t.kind) { case '!': { double d = left; left = 1; double limit = d; for (int i = 0; i < limit; ++i) { left *= d; d = d - 1; } t = ts.get(); break; } case '/': { double d = primary(); if (d == 0) error("division by zero"); left /= d; t = ts.get(); break; } case '*': left *= primary(); t = ts.get(); break; default: ts.putback(t); return left; } } 轻松地对其进行测试,这将为一个调用设置环境。如果可行,您有一个解决方法,下面将介绍一种更永久的方法。

我已解决的当前方法是将MIX_ENV修改为如下所示:

MIX_ENV=test mix test

此处与默认mix.exs不同的重要位是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 块中mix.exs的定义(这应该已经匹配,但是如果不匹配,则应该),并添加行elixirrc_paths

这可能不是完全习惯,但是在使用开发混合环境时,它将确保您的测试也已编译,并且仍然允许您分别定义开发和测试环境。