我的测试未能说Rumbl.Routers.Helpers中不存在page_path函数

时间:2017-04-27 20:52:51

标签: elixir phoenix-framework

所以我似乎无法弄清楚为什么我的测试失败了。

错误信息是:

1) test require authentication on all actions (Rumbl.VideoControllerTest)
     test/controllers/video_controller_test.exs:4
     ** (UndefinedFunctionError) function Rumbl.Routers.Helpers.page_path/2 is undefined (module Rumbl.Routers.Helpers is not available)
     stacktrace:
       Rumbl.Routers.Helpers.page_path(%Plug.Conn{adapter: {Plug.Adapters.Test.Conn, :...}, assigns: %{current_user: nil}, before_send: [#Function<0.101282891/1 in Plug.CSRFProtection.call/2>, #Function<4.74055627/1 in Phoenix.Controller.fetch_flash/2>, #Function<0.131660147/1 in Plug.Session.before_send/2>, #Function<1.60643777/1 in Plug.Logger.call/2>], body_params: %{}, cookies: %{}, halted: false, host: "www.example.com", method: "GET", owner: #PID<0.416.0>, params: %{}, path_info: ["manage", "videos", "new"], path_params: %{}, peer: {{127, 0, 0, 1}, 111317}, port: 80, private: %{Rumbl.Router => {[], %{}}, :phoenix_endpoint => Rumbl.Endpoint, :phoenix_flash => %{}, :phoenix_format => "html", :phoenix_pipelines => [:browser, :authenticate_user], :phoenix_recycled => true, :phoenix_route => #Function<15.17895686/1 in Rumbl.Router.match_route/4>, :phoenix_router => Rumbl.Router, :plug_session => %{}, :plug_session_fetch => :done, :plug_skip_csrf_protection => true}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{}, req_headers: [], request_path: "/manage/videos/new", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "ve978tecm111nf993e7nnjd9atl5892j"}, {"x-frame-options", "SAMEORIGIN"}, {"x-xss-protection", "1; mode=block"}, {"x-content-type-options", "nosniff"}], scheme: :http, script_name: [], secret_key_base: "509oRqKQNK7D/P43t6KZhu5jMhkWEJF455n+TS/Dve84JTYENxW08EqFwXedE9SI", state: :unset, status: nil}, :index)
       (rumbl) web/controllers/auth.ex:34: Rumbl.Auth.authenticate_user/2
       (rumbl) web/router.ex:1: Rumbl.Router.match_route/4
       (rumbl) web/router.ex:1: Rumbl.Router.do_call/2
       (rumbl) lib/rumbl/endpoint.ex:1: Rumbl.Endpoint.phoenix_pipeline/1
       (rumbl) lib/rumbl/endpoint.ex:1: Rumbl.Endpoint.call/2
       (phoenix) lib/phoenix/test/conn_test.ex:224: Phoenix.ConnTest.dispatch/5
       test/controllers/video_controller_test.exs:6: (test)

我的单元测试如下:

defmodule Rumbl.VideoControllerTest do
  use Rumbl.ConnCase

  test "require authentication on all actions", %{conn: conn} do
    Enum.each([
        get(conn, video_path(conn, :new)),
        get(conn, video_path(conn, :index)),
        get(conn, video_path(conn, :show, "123")),
        get(conn, video_path(conn, :edit, "123")),
        put(conn, video_path(conn, :update, "123", %{})),
        post(conn, video_path(conn, :create, %{})),
        delete(conn, video_path(conn, :delete, "123")),
      ], fn conn ->
      assert html_response(conn, 302)
      assert conn.halted
    end)
  end
end

conn_case.ex文件:

defmodule Rumbl.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 and query models.

  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

      alias Rumbl.Repo
      import Ecto
      import Ecto.Changeset
      import Ecto.Query

      import Rumbl.Router.Helpers
      import Rumbl.TestHelpers

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

  setup tags do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Rumbl.Repo)

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(Rumbl.Repo, {:shared, self()})
    end

    {:ok, conn: Phoenix.ConnTest.build_conn()}
  end
end

有什么突出的吗?

另外请评论你将如何诊断这个?

2 个答案:

答案 0 :(得分:2)

错误消息表明找不到模块Rumbl.Routers.Helpers。通常,Phoenix Router(无s)模块不是多元化的。此外,您在conn情况下引用import Rumbl.Router.Helpers。因此,您似乎已在项目中的某处导入或别名Rumbl.Routers

如果您在项目中搜索Routers,则应该找到拼写错误。

答案 1 :(得分:0)

尝试以下命令,如果输出中没有page_path,则可以解释该错误,您可以添加page_path(通过在PageController中添加def index(。)方法)

$ mix phx.routes

page_path GET / HrcPhxWeb.PageController:index ...