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

Hello. Recently, I’ve been working on updating a legacy repo. I’ve had to update Erlang, Elixir, Phoenix and a lot of other dependencies. The repo is most likely going to be deprecated in the near future so I’ve tried to avoid doing too much refactoring. I currently have 1 test that is failing and I cannot figure out how to fix this.

The test checks to see if my 404.html template is rendered when hitting a route the doesn’t exist.
The test:

  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"

When I spin the server up and hit a non-existent endpoint through Postman, I get the response I expect. When I run the test in my terminal, I get this:

1) test renders 404.html (MyApp.ErrorViewTest)
** (Phoenix.Router.NoRouteError) no route found for GET /fake (MyApp.Router)
code: conn = get build_conn(), "/fake"
  (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

Here is my ErrorView:

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

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

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

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

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

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

  # 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

I’m currently using Phoenix 1.5.5, Elixir 1.13.1, Erlang 24.1.7

Any help would be greatly appreciated here.

I think you’re looking for assert_error_sent/2.

1 Like

This ended up solving the problem for me! Thank you.

1 Like