optikfluffel
URL Router Helpers not working in tests
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?
Marked As Solved
LostKobrakai
The conn you get from MyApp.ConnTest is 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.
Also Liked
NobbZ
I’m at home now and therefore have a bit more time to actually take a look ![]()
The conn in 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.
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…
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










