繁体   English   中英

** (Phoenix.Router.NoRouteError) 找不到 GET /fake (MyApp.Router) 的路由

[英]** (Phoenix.Router.NoRouteError) no route found for GET /fake (MyApp.Router)

最近,我一直在努力更新旧版存储库。 我不得不更新 Erlang、Elixir、Phoenix 和许多依赖项。 该 repo 很可能会在不久的将来被弃用,因此我尽量避免进行过多的重构,即使这可能是必要的。 我目前有 1 个测试失败,我不知道如何解决这个问题。

测试检查我的 404.html 模板是否在点击不存在的路线时呈现。

考试:

  test "renders 404.html" do
    conn = get build_conn(), "/fake"
    assert html_response(conn, 404) ==
      "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\">\n    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n    <meta name=\"description\" content=\"\">\n    <meta name=\"author\" content=\"\">\n    <link rel=\"stylesheet\" href=\"http://localhost/css/index.css\">\n    <title>404 - Page Not Found</title>\n  </head>\n\n  <body class=\"error-body\">\n    <div class=\"error-div\">\n      <img class=\"error-img\" src=\"/images/error_dolphin.gif\">\n      <h1>Error 404!</h1>\n      <p class=\"error-p\">Hmmm... the page you're looking for is missing.</p>\n      <button class=\"error-button\">\n        <a class=\"error-a\" href=\"/dashboard\">Go to Homepage</a>\n      </button>\n    </div>\n  </body>\n</html>\n"
  end

当我启动服务器并通过 Postman 访问一个不存在的端点时,我得到了我期望的响应。 当我在终端中运行测试时,我得到了这个:

1) test renders 404.html (MyApp.ErrorViewTest)
test/views/error_view_test.exs:4
** (Phoenix.Router.NoRouteError) no route found for GET /fake (MyApp.Router)
code: conn = get build_conn(), "/fake"
stacktrace:
  (MyApp 0.0.1) lib/phoenix/router.ex:402: MyApp.Router.call/2
  (MyApp 0.0.1) lib/MyApp/endpoint.ex:1: MyApp.Endpoint.plug_builder_call/2
  (MyApp 0.0.1) lib/MyApp/endpoint.ex:1: MyApp.Endpoint.call/2
  (phoenix 1.5.13) lib/phoenix/test/conn_test.ex:225: Phoenix.ConnTest.dispatch/5
  test/views/error_view_test.exs:5: (test)


Finished in 0.06 seconds (0.06s async, 0.00s sync)
1 test, 1 failure

这是我的错误视图:

defmodule MyApp.ErrorView do
  use MyApp.Web, :view

  def render("500.json", _assigns) do
    %{error: "Error"}
  end

  def render("404.json", _assigns) do
    %{error: "Not Found"}
  end

  def render("401.json", _assigns) do
    %{error: "Unauthenticated"}
  end

  def render("403.json", _assigns) do
    %{error: "Unauthorized"}
  end

  def render("400.json", %{error: error}) do
    %{error: error}
  end

  # In case no render clause matches or no
  # template is found, let's render it as 500
  def template_not_found(_template, assigns) do
    render "500.html", assigns
  end
end

我目前正在使用 Phoenix 1.5.5、Elixir 1.13.1、Erlang 24.1.7

任何帮助将不胜感激。

assert_error_sent/2get包裹在你的测试中,你应该很高兴:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM