optikfluffel

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

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

NobbZ

I’m at home now and therefore have a bit more time to actually take a look :smiley:

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

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…

Where Next?

Popular in Questions Top

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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
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
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

We're in Beta

About us Mission Statement