ChipCoons
Routes.Helper.path/3 is undefined error in test
I’m having some further problems with the generated tests for my controller.
I ran
mix phx.gen.json Propsects Person persons {with my data parameters} --web Prospects
and the migration, schema and controller files (with tests) were generated. I added the resource to my Router and ran mix.phx.routes to verify with the following results:
mix phx.routes
page_path GET / ScandimensionWeb.PageController :index
admin_path GET /admin ScandimensionWeb.AdminController :index
user_path GET /api/users ScandimensionWeb.UserController :index
user_path GET /api/users/:id ScandimensionWeb.UserController :show
user_path POST /api/users ScandimensionWeb.UserController :create
user_path PATCH /api/users/:id ScandimensionWeb.UserController :update
PUT /api/users/:id ScandimensionWeb.UserController :update
user_path DELETE /api/users/:id ScandimensionWeb.UserController :delete
user_path POST /api/users/sign_in ScandimensionWeb.UserController :sign_in
person_path GET /prospects/persons ScandimensionWeb.PersonController :index
person_path GET /prospects/persons/:id/edit ScandimensionWeb.PersonController :edit
person_path GET /prospects/persons/new ScandimensionWeb.PersonController :new
person_path GET /prospects/persons/:id ScandimensionWeb.PersonController :show
person_path POST /prospects/persons ScandimensionWeb.PersonController :create
person_path PATCH /prospects/persons/:id ScandimensionWeb.PersonController :update
PUT /prospects/persons/:id ScandimensionWeb.PersonController :update
person_path DELETE /prospects/persons/:id ScandimensionWeb.PersonController :delete
websocket WS /socket/websocket ScandimensionWeb.UserSocket
When I run mix test, the new PersonControllerTest fails on each call to:
Routes.prospects_person_path(conn, :index))
with the following error message:
** (UndefinedFunctionError) function ScandimensionWeb.Router.Helpers.prospects_person_path/2 is undefined or private
code: conn = post(conn, Routes.prospects_person_path(conn, :create), person: @create_attrs)
stacktrace:
(scandimension) ScandimensionWeb.Router.Helpers.prospects_person_path(%Plug.Conn{adapter: {Plug.Adapters.Test.Conn, :…}, assigns: %{}, before_send: , body_params: %Plug.Conn.Unfetched{aspect: :body_params}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: “www.example.com”, method: “GET”, owner: pid<0.439.0>, params: %Plug.Conn.Unfetched{aspect: :params}, path_info: , path_params: %{}, port: 80, private: %{phoenix_recycled: true, plug_skip_csrf_protection: true}, query_params: %Plug.Conn.Unfetched{aspect: :query_params}, query_string: “”, remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{“accept”, “application/json”}], request_path: “/”, resp_body: nil, resp_cookies: %{}, resp_headers: [{“cache-control”, “max-age=0, private, must-revalidate”}], scheme: :http, script_name: , secret_key_base: nil, state: :unset, status: nil}, :create)
test/scandimension_web/controllers/prospects/person_controller_test.exs:43: (test)
I checked the helpers via iex:
iex(4)> ScandimensionWeb.Router.Helpers.
admin_path/2 admin_path/3 admin_url/2 admin_url/3
page_path/2 page_path/3 page_url/2 page_url/3
path/2 person_path/2 person_path/3 person_path/4
person_url/2 person_url/3 person_url/4 static_path/2
static_url/2 url/1 user_path/2 user_path/3
user_path/4 user_url/2 user_url/3 user_url/4
And agree that prospects_person_path is not defined, but I’m not certain how to fix this.
Any guidance?
Thanks,
Marked As Solved
mikemccall
The router needs to know about the namespace.
Currently you have
scope "/prospects", ScandimensionWeb, as: "prospects" do
pipe_through :api
resources "/persons", PersonController
end
And this gets you the route helper ![]()
But if you look at it, it is namespaced to ScandimensionWeb.PersonController when it needs to be ScandimensionWeb.Prospects.PersonController in regards to the controller module.
defmodule ScandimensionWeb.Prospects.PersonController do
use ScandimensionWeb, :controller
...
end
Try
scope "/prospects", ScandimensionWeb, as: "prospects" do
pipe_through :api
resources "/persons", Prospects.PersonController
end
or
scope "/", ScandimensionWeb do
scope "/prospects", Prospects, as: "prospects" do
pipe_through :api
resources "/persons", PersonController
end
end
Also Liked
ChipCoons
Adding the namespace for the resources did the trick. Thank you!
mikemccall
True, but from what I can tell using the alias with scope will always append. That is why @ChipCoons got this error.
** (UndefinedFunctionError) function ScandimensionWeb.ScandimensionWeb.Prospects.PersonController.init/1 is undefined (module ScandimensionWeb.ScandimensionWeb.Prospects.PersonController is not available)
Hence the double ScandimensionWeb.
Another option would be leaving the alias out of the scope declaration such as:
alias ScandimensionWeb.Prospects.PersonController
...
scope path: "/prospects", as: "prospects" do
pipe_through :api
resources "/persons", PersonController
end
Which I think is what you were getting at? There’s definitely more than one way to skin a cat with the phoenix router.
I guess technically this works too:
scope "/prospects", ScandimensionWeb, as: "prospects" do
pipe_through :api
alias Prospects.PersonController
resources "/persons", PersonController
end
But I’m not sure I’m a fan of nesting the alias.
ChipCoons
I actually decided for this app that namespacing was not needed, and reverted back to an earlier version and re-ran the mix generate command without the --Web flag for name spacing. Thank you all for your assistance. I’m sure I’ll run across something else as I try to pick up Phoenix and Elixir.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









