# Phoenix - testing JSON Api

**URL:** <https://forum.elixirforum.com/t/phoenix-testing-json-api/42788>\
**Category:** Questions\
**Tags:** phoenix, testing, troubleshooting, rest\_api\
**Created:** [October 1, 2021, 6:24pm UTC](https://forum.elixirforum.com/t/phoenix-testing-json-api/42788 "2021-10-01T18:24:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wwaldner](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/w/ad7895/32.png) [@wwaldner](https://forum.elixirforum.com/u/wwaldner)\
**Post date:** [October 1, 2021, 6:24pm UTC](https://forum.elixirforum.com/t/phoenix-testing-json-api/42788/1 "2021-10-01T18:24:30Z")

</div>

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

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

```

My test

```elixir
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.

---

<div class="post-metadata">

**Author:** ![stefanchrobot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/32/7657_2.png) [@stefanchrobot](https://forum.elixirforum.com/u/stefanchrobot)\
**Post date:** [October 1, 2021, 7:22pm UTC](https://forum.elixirforum.com/t/phoenix-testing-json-api/42788/2 "2021-10-01T19:22:56Z")

</div>

Seems that you’re looking for [`assert_error_sent`](https://hexdocs.pm/phoenix/Phoenix.ConnTest.html#assert_error_sent/2).

---

<div class="post-metadata">

**Author:** ![wwaldner](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/w/ad7895/32.png) [@wwaldner](https://forum.elixirforum.com/u/wwaldner)\
**Post date:** [October 2, 2021, 7:43pm UTC](https://forum.elixirforum.com/t/phoenix-testing-json-api/42788/3 "2021-10-02T19:43:42Z")

</div>

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.

```elixir
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
