模块ConnCase未加载,找不到

问题描述 投票:1回答:1

无法确定此错误。

我有这个文件:

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并找不到]

testing phoenix-framework mix
1个回答
0
投票

对于遇到此问题的其他人,您可能遇到过与我相同的问题-> MIX_ENV默认情况下设置为dev。在这种情况下,您可以通过运行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块中def project的定义,以及行defp elixirc_paths(:dev), do: ["lib", "test/support"]的添加

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

© www.soinside.com 2019 - 2024. All rights reserved.