Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(RuntimeError) expected connection to have a response

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?

like image 986
stoft Avatar asked May 05 '15 21:05

stoft


1 Answers

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>
like image 111
DevL Avatar answered Oct 09 '22 08:10

DevL