silverdr

silverdr

Testing conn helpers in isolation?

Let’s say I have a helper function called not_found/1, which looks something like:

def not_found(conn) do
	conn
	|> put_status(404)
	|> put_view(MyApplication.ErrorView)
	|> render(:"404")
	|> halt()
end

This is a very trivial example to show the idea. Now I wonder how to test such things in isolation? Like how would setup / assertions need to look like.

Marked As Solved

LostKobrakai

LostKobrakai

Yeah, that was a “typo” on my part.

build_conn gives you a “bare” conn. Your plug requires a format to be available on the conn (when using render with an atom), so you need to set a format on the conn as well: conn = put_format(conn, "html"). Usually this is set by the Phoenix.Controller.accepts/2 plug in your router pipeline.

Also Liked

LostKobrakai

LostKobrakai

Something akin to that should work in a normal phoenix MyAppWeb.ConnCase module:

    test "no header" do
      conn = build_conn(:get, "/")

      conn = call_plug(conn, [])

      assert conn.status == 403
      assert conn.state == :sent
      assert conn.halted
    end

Last Post!

silverdr

silverdr

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement