ChipCoons

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

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 :+1:

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

ChipCoons

Adding the namespace for the resources did the trick. Thank you!

mikemccall

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

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.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement