I'm a new user to Phoenix Framework and I'm trying to set up a simple HTTP POST service, that performs a calculation on incoming data and returns the result but I'm getting the following error:
** (RuntimeError) expected connection to have a response but no response was set/sent
stacktrace:
(phoenix) lib/phoenix/conn_test.ex:311: Phoenix.ConnTest.response/2
(phoenix) lib/phoenix/conn_test.ex:366: Phoenix.ConnTest.json_response/2
test/controllers/translation_controller_test.exs:20
My test case:
test "simple POST" do
post conn(), "/api/v1/foo", %{"request" => "bar"}
IO.inspect body = json_response(conn, 200)
end
My router definition:
scope "/api", MyWeb do
pipe_through :api
post "/v1/foo", TranslationController, :transform
end
My controller:
def transform(conn, params) do
doc = Map.get(params, "request")
json conn, %{"response" => "grill"}
end
What am I missing?
In your test you use Plug.Test.conn/4
to get a Plug.Conn
struct and pass it as an argument to post
. However, you don't store the result in a variable named conn
.
This means that the second use of conn
, when inspecting the json_response
is actually a second call to Plug.Test.conn/4
.
Try this instead:
test "simple POST" do
conn = post conn(), "/api/v1/foo", %{"request" => "bar"}
assert json_response(conn, 200) == <whatever the expected JSON should be>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With