Error on default test after added Pow

I added pow to an existing project and, of course, all tests failed.
So I found this topic: https://elixirforum.com/t/controller-test-with-pow/ which helped me so much!

However I still getting 2 errors:

1) test index lists all admins (ContsWeb.AdminControllerTest)
     test/conts_web/controllers/admin_controller_test.exs:27
     ** (Ecto.Query.CastError) deps/ecto/lib/ecto/repo/queryable.ex:451: value `"admins"` cannot be dumped to type :binary_id in query:

     from u0 in Conts.Accounts.User,
       where: u0.id == ^"admins",
       select: u0

     stacktrace:
       (elixir 1.11.0) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
       (elixir 1.11.0) lib/enum.ex:1521: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (elixir 1.11.0) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ecto 3.5.5) lib/ecto/repo/queryable.ex:213: Ecto.Repo.Queryable.execute/4
       (ecto 3.5.5) lib/ecto/repo/queryable.ex:17: Ecto.Repo.Queryable.all/3
       (ecto 3.5.5) lib/ecto/repo/queryable.ex:149: Ecto.Repo.Queryable.one/3
       (conts 0.1.0) lib/conts/accounts.ex:92: Conts.Accounts.get/2
       (conts 0.1.0) lib/conts_web/controllers/user_controller.ex:30: ContsWeb.UserController.show/2
       (conts 0.1.0) lib/conts_web/controllers/user_controller.ex:1: ContsWeb.UserController.action/2
       (conts 0.1.0) lib/conts_web/controllers/user_controller.ex:1: ContsWeb.UserController.phoenix_controller_pipeline/2

and

2) test update user redirects when data is valid (ContsWeb.UserControllerTest)
     test/conts_web/controllers/user_controller_test.exs:64
     ** (RuntimeError) expected redirection with status 302, got: 200
     code: assert redirected_to(conn) == Routes.user_path(conn, :show, user)
     stacktrace:
       (phoenix 1.5.7) lib/phoenix/test/conn_test.ex:450: Phoenix.ConnTest.redirected_to/2
       test/conts_web/controllers/user_controller_test.exs:66: (test)

and these are the respective tests and functions:

# admin_controller_test.exs

  describe "index" do
    test "lists all admins", %{authed_conn: conn} do
      conn = get(conn, Routes.admin_path(conn, :index))
      assert html_response(conn, 200) =~ "Listing Admins"
    end
  end

# accounts.ex

  def list(module) do
    Repo.all(module)
  end

  @doc """
  Gets a single entity.
  """
  def get(module, id) do
    case Repo.get(module, id) do
      nil ->
        {:error, :not_found}

      entity ->
        {:ok, entity}
    end
  end

# user_controller_test.exs

    test "redirects when data is valid", %{authed_conn: conn, user: user} do
      conn = put(conn, Routes.user_path(conn, :update, user), user: @update_attrs)
      assert redirected_to(conn) == Routes.user_path(conn, :show, user)

      conn = recycle_conn(conn)

      conn = get(conn, Routes.user_path(conn, :show, user))
      assert html_response(conn, 200) =~ "another@gmail.com"
    end

# admin_controller.ex

  def index(conn, _params) do
    admins = Accounts.list(Admin)
    render(conn, "index.html", admins: admins)
  end

could you help me?

Ok, I solved! The first error, was because of the order of routes…

I made like:

scope "/users" do
  resources "/", ...
  resources "/admins", ...
end

this that when I tried to GET the index route for admins, the router pattern matched as /users/admins with the first resource, so was interpreted as /users/:id… changing the orders solved the problem.

The second one was that current_password was missing on the @update_attrs

1 Like