8

After

iex -S mix phx.server

I want to do some quick tests in the iex terminal, but some functions require the struct %Plug.Conn{} as an argument, for example I wanted to get the result of expression:

MyAppWeb.Router.Helpers.confirmation_url(%Plug.Conn{}, :edit, "12345")

But I've got error:

Phoenix endpoint not found in %{}

Is there a simple way of getting conn struct in the console?

AndreyKo
  • 1,421
  • 2
  • 11
  • 25

3 Answers3

15

Router helper functions accept either a conn or an endpoint module as the first argument. You can pass the endpoint module of your app when you want to generate a URL without a conn:

MyAppWeb.Router.Helpers.confirmation_url(MyAppWeb.Endpoint, :edit, "12345")

Edit: If you want to create a dummy conn that works with Router helpers, it seems like it's enough to put a %{phoenix_endpoint: MyAppWeb.Endpoint} value in conn.private as of Phoenix 1.3:

conn = %Plug.Conn{private: %{phoenix_endpoint: MyAppWeb.Endpoint}}
MyAppWeb.Router.Helpers.confirmation_url(conn, :edit, "12345")
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • @ Dogbert: Thanks! It helped in this particular case, but for other functions this could be unsufficient, can we construct valid conn struct easily? – AndreyKo Aug 27 '17 at 17:22
  • I couldn't find any function that fills in `phoenix_endpoint` for you automatically. I explicitly put my Endpoint module in a conn like this and it works with the Router helper functions: `%Plug.Conn{private: %{phoenix_endpoint: MyApp.Endpoint}} |> MyApp.Router.Helpers.page_url(:index)`. – Dogbert Aug 27 '17 at 19:43
  • It worked great, can you add it to your answer for me to accept it? – AndreyKo Aug 28 '17 at 04:18
  • 1
    I also thought that we can bind conn = %Plug.Conn{private: %{phoenix_endpoint: MyApp.Endpoint}} variable in .iex.exs file in the project's root directory and it will be available in the console any time, may be it's worth mentioning in your answer too? – AndreyKo Aug 29 '17 at 04:58
  • While this would work for `MyAppWeb.Router.Helpers.confirmation_url`, this won't for `MyAppWeb.Router.Helpers.static_url(endpoint, path)` :( Any idea how to achieve this there? – Augustin Riedinger Jul 16 '20 at 08:44
5

The ConnCase test helpers use Phoenix.ConnTest.build_conn() to bootstrap a connection struct for the controller tests.

You can find the function here and either use it directly or follow its implementation and tweak it as you like.

Phillipp
  • 1,425
  • 2
  • 12
  • 27
  • I'm running the console alongside the server with `iex -S mix phx.server`. I'm looking to grab the current connection being used and edit it. How can I do this? – Thomas May 27 '22 at 15:47
-1

Why spending time with testing on the console. Just write a test and use the 'ConnCase' which gives you the conn struct in your tests for free. During development you can also use the "test watch" package which will rerun your tests on every file change.

As soon as you switch to tdd as more time you will save with problems like this

webdeb
  • 12,993
  • 5
  • 28
  • 44
  • 2
    In this particular case I just wanted to find out the url generated by third party library, I think in some cases quick testing in console may be valuable. – AndreyKo Aug 28 '17 at 04:21