Help resolve ** (RuntimeError) expected redirection with status 302, got: 200

Hello, I keep getting this obscure error when testing my phoenix app:

mix test test/wesortix_web/controllers/geoname_controller_test.exs

  1) test create geoname redirects to show when data is valid (WesortixWeb.GeonameControllerTest)
     ** (RuntimeError) expected redirection with status 302, got: 200
     code: assert %{id: id} = redirected_params(conn)
       (phoenix 1.6.10) lib/phoenix/test/conn_test.ex:450: Phoenix.ConnTest.redirected_to/2
       (phoenix 1.6.10) lib/phoenix/test/conn_test.ex:585: Phoenix.ConnTest.redirected_params/1
       test/wesortix_web/controllers/geoname_controller_test.exs:29: (test)

  2) test update geoname redirects when data is valid (WesortixWeb.GeonameControllerTest)
     ** (RuntimeError) expected redirection with status 302, got: 200
     code: assert redirected_to(conn) == Routes.geoname_path(conn, :show, geoname)
       (phoenix 1.6.10) lib/phoenix/test/conn_test.ex:450: Phoenix.ConnTest.redirected_to/2
       test/wesortix_web/controllers/geoname_controller_test.exs:56: (test)


Finished in 0.2 seconds (0.00s async, 0.2s sync)
8 tests, 2 failures

The test code is boilerplate from a mix.gen.html task. I have also created other models with the same mix.gen.html task and their equivalent ControllerTests work fine. See relevant code fragments here:

# genome_controller_test.exs

25   describe "create geoname" do
26    test "redirects to show when data is valid", %{conn: conn} do
27      conn = post(conn, Routes.geoname_path(conn, :create), geoname: @create_attrs)
29      assert %{id: id} = redirected_params(conn)
30      assert redirected_to(conn) == Routes.geoname_path(conn, :show, id)
32      conn = get(conn, Routes.geoname_path(conn, :show, id))
33      assert html_response(conn, 200) =~ "Show Geoname"
34    end
40 end

51  describe "update geoname" do
52    setup [:create_geoname]
54    test "redirects when data is valid", %{conn: conn, geoname: geoname} do
55      conn = put(conn, Routes.geoname_path(conn, :update, geoname), geoname: @update_attrs)
56      assert redirected_to(conn) == Routes.geoname_path(conn, :show, geoname)
58      conn = get(conn, Routes.geoname_path(conn, :show, geoname))
59      assert html_response(conn, 200) =~ "some updated admin1_code"
60    end
66 end

So far I have established that the error occurs on the first assert line, in other words, the code never gets to the “assert html_response(conn, 200)” command, but I don’t know how to debug that as it dives right into boilerplate phoenix code that does pass in the other model tests.

The only thing I can think of that is different between the various models is that geoname has had some name changes to its fields (and an id column that gets imported, not auto incremented) whereas the others have not.

How do I start resolving this?

1 Like

It sounds like one of these changes has either broken the create action in the GeonameController, or caused the parameters in @create_attrs to not result in a valid Geoname - thus the generated code is showing the form with errors (and status 200) instead of redirecting to the created resource.