问题
I created a controller in my Phoenix application called ProgressController
. This is what my router file look like:
defmodule MyTestApp.Router do
use MyTestApp.Web, :router
pipeline :api do
plug :accepts, ["json"]
end
scope "/", MyTestApp do
pipe_through :api
get "/users/:user_id/courses/:course_id", ProgressController, :show
end
end
When I run mix phoenix.routes
it outputs:
progress_path GET /users/:user_id/courses/:course_id MyTestApp.ProgressController :show
And I have the following test, which uses progress_path
:
test "shows the user's progress in the given course", %{conn: conn} do
# prepare some stuff
conn = get conn, progress_path(conn, :show, %{user_id: 1, course_id: 7})
# assert some stuff
end
progress_path
is the same name that mix phoenix.routes
gave me, and I am surprise do see this error message when running my tests:
Compiling 11 files (.ex)
** (CompileError) test/controllers/progress_controller_test.exs:12: undefined function progress_path/3
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:363: Code.require_file/2
(elixir) lib/kernel/parallel_require.ex:57: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5
20:34:46.402 [error] GenServer #PID<0.242.0> terminating
** (CompileError) test/controllers/progress_controller_test.exs:12: undefined function progress_path/3
(stdlib) lists.erl:1338: :lists.foreach/2
(elixir) src/elixir_module.erl:113: :elixir_module.do_compile/5
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) src/elixir.erl:223: :elixir.erl_eval/3
(elixir) src/elixir.erl:211: :elixir.eval_forms/4
(elixir) src/elixir_compiler.erl:66: :elixir_compiler.eval_compilation/3
(elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/3
(elixir) src/elixir_compiler.erl:30: :elixir_compiler.quoted/3
(elixir) lib/code.ex:363: Code.require_file/2
(elixir) lib/kernel/parallel_require.ex:57: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5
I narrowed down the problem to the fact that there are 2 params in that route's URL, but I can't figure out how to actually fix this. What am I doing wrong?
回答1:
After a lot of search, trial and errors, I discovered that the path
function expects a list of params, not a Map
. Like this:
conn = get conn, progress_path(conn, :show, 1, 7)
回答2:
I found a solution looking at the last documentation.
To take the path_helper you have to set:
Routes.page_path(@conn, :name)
来源:https://stackoverflow.com/questions/39812524/undefined-controller-path-for-route-with-two-params