wwaldner

wwaldner

Phoenix - testing JSON Api

I am trying to write some tests for a JSON Api. If something in the action raises an error, I would like a JSON error to be returned to my test.

Action

def get(conn, params) do
   1=2
end

My test

test "Get always returns 500 - internal server error", %{conn: conn} do
   conn = get(conn, Routes.my_path(conn, :get_it)
   assert json_response(conn, 500)["errors"] != %{}
end

test raises an error: ‘no mach of right hand…’ Instead of it raising an error, I would like to capture a 500 error similar to what the client will get.

Most Liked

stefanchrobot

stefanchrobot

Seems that you’re looking for assert_error_sent.

Last Post!

wwaldner

wwaldner

Yes, that was it. Coupled with my lack of understand of how to return a non-200 result code back to client. I think i have this figured out now.

conn
|> put_status(:not_found)
|> json(%{"Errors" => "Not found"})

Will allow the client/test to receive a normal HTTP response. If an error is raised, like a match error, that will automatically return a 500 result code and the test has to use the assert_error_sent

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement