(RuntimeError) expected connection for response

I am a new user of the Phoenix Framework, and I am trying to configure a simple HTTP POST service that calculates the input data and returns the result, but I get 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?

+8
elixir phoenix-framework
source share
1 answer

In your test, you use Plug.Test.conn/4 to get the Plug.Conn structure and pass it as the post argument. However, you do not save the result in a variable named conn .

This means that the second use of conn when checking json_response actually the 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> 
+11
source share

All Articles