optikfluffel
Having a View, with a function using the Routes.*_url helper, everything works fine, until I try to test this function directly.
defmodule ExampleWeb.PageView do
use ExampleWeb, :view
def url_helper(conn, slug) do
Routes.page_url(conn, :show, slug)
end
end
defmodule ExampleWeb.PageViewTest do
use ExampleWeb.ConnCase, async: true
alias ExampleWeb.PageView
test "url_helper/2", %{conn: conn} do
slug = "my-example"
assert PageView.url_helper(conn, slug) == "http://localhost:4002/my-example"
end
end
Result when running the test:
1) test url_helper/2 (ExampleWeb.PageViewTest)
test/example_web/views/page_view_test.exs:12
** (CaseClauseError) no case clause matching: %{phoenix_recycled: true, plug_skip_csrf_protection: true}
code: assert PageView.url_helper(conn, slug) == "http://localhost:4000/my-example"
stacktrace:
(phoenix 1.5.3) lib/phoenix/router/helpers.ex:18: Phoenix.Router.Helpers.url/2
(example 0.1.0) lib/example_web/router.ex:1: ExampleWeb.Router.Helpers.page_url/4
test/example_web/views/page_view_test.exs:15: (test)
If I change this to use the Routes.*_path helper everything works as intended, but I need the complete and absolute urls form time to time.
Did I miss anything when setting up the conn for testing?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
What is the endpoints configuration for
:test, especially the:urlfield?optikfluffel
I’m pretty sure those are just the ones generated by
mix phx.new, nothing fancy or custom yet.in
config.exs:in
test.exs:NobbZ
I’m at home now and therefore have a bit more time to actually take a look
The
connin oyur tests is a connectiont that is created from thin air and has no associated endpoint. Though the endpoint is required for the URL helpers to actually create the URL.You can use
Phoenix.ConnTest.bypass_through/x(IIRC) to make that stub connection a “full” one with the sideeffect of it passing through the full pipeline stack, which might make the test require a significant amount more time.optikfluffel
Thanks, that’s exactly what I’m doing right now.
I just hoped there was a way to test a View without actually calling a Route I didn’t knew about. This still feels a little wrong in my opinion.
NobbZ
To be honest, I don’t really understand, why you need to test the URL helpers. That is basically testing if the path helpers do their job while tightly coupling test results to certain config values…
optikfluffel
I just trimmed this down to have a somewhat minimal example here. In my real app I’m of course not only having the URL helper in there, I have a function that uses them to create an RSS feed. I wanted to generate this in the view, so I have a nice and clean controller like:
in my View I then have a little function that turns a Post into an Item for rendering the xml and I need the full urls for that.
dimitarvp
That still doesn’t answer the question why you would basically unit-test Phoenix itself.
The URL helpers are working, you can be very sure of it.
optikfluffel
I don’t want to unit-test Phoenix or the url helper itself. I just want to test a function in one of my views that happens to use one of those helpers, because I need absolute urls in there.
LostKobrakai
The conn you get from
MyApp.ConnTestis not linked to any particular endpoint. You could have multiples of those in your project. Therefore you need to link that conn (basically a plain request) to an endpoint before you can use it to unit test things depending on a conn for a specific endpoint. Without that linkage the url generation doesn’t know which host to use, which path was requested, if the endpoint was mounted to a subfolder, …For most ConnTests those things are set when you do
get conn, path, …. but for unit tests you need to handle it on your own.messutied
Hi LostKobrakai, when you say “handle it on your own”, do you mean basically setting the private :phoenix_endpoint in conn manually?
Looking at Phoenix.ConnTest — Phoenix v1.8.8 it would seem like setting a
@endpoint MyAppWeb.HomeControllerwould suffice but it does not.