title: Elixir Phoenix: Strange "no function clause matching" error in Controller Test
body: <p>My tests for a controller are failing and I cannot figure out the root of this error. I tried everything I could think of and am a bit helpless since the error message is quite generic.</p>
  
  <p>The error message in question (when running the tests).</p>
  
  <pre><code>1) test does not create resource and renders errors when data is invalid (MyApp.AdminControllerTest)
     test/controllers/admin_controller_test.exs:21
     ** (FunctionClauseError) no function clause matching in MyApp.AdminControllerTest.__ex_unit_setup_1/1
     stacktrace:
       test/controllers/admin_controller_test.exs:8: MyApp.AdminControllerTest.__ex_unit_setup_1(%{async: false, case: MyApp.AdminControllerTest, file: "~/MyApp/test/controllers/admin_controller_test.exs", line: 21, test: :"test does not create resource and renders errors when data is invalid"})
       test/controllers/admin_controller_test.exs:1: MyApp.AdminControllerTest.__ex_unit__/2
  
  
  
  2) test creates and renders resource when data is valid (MyApp.AdminControllerTest)
     test/controllers/admin_controller_test.exs:12
     ** (FunctionClauseError) no function clause matching in MyApp.AdminControllerTest.__ex_unit_setup_1/1
     stacktrace:
       test/controllers/admin_controller_test.exs:8: MyApp.AdminControllerTest.__ex_unit_setup_1(%{async: false, case: MyApp.AdminControllerTest, file: "~/MyApp/test/controllers/admin_controller_test.exs", line: 12, test: :"test creates and renders resource when data is valid"})
       test/controllers/admin_controller_test.exs:1: MyApp.AdminControllerTest.__ex_unit__/2
  </code></pre>
  
  <p><strong>My Code:</strong></p>
  
  <p>admin_controller_test.exs</p>
  
  <pre><code>defmodule MyApp.AdminControllerTest do
    use MyApp.ConnCase
  
    alias MyApp.Admin
    @valid_attrs %{email: "foo@bar.com", password: "s3cr3t"}
    @invalid_attrs %{}
  
    setup %{conn: conn} do
      {:ok, conn: put_req_header(conn, "accept", "application/json")}
    end
  
    test "creates and renders resource when data is valid", %{conn: conn} do
      conn = post conn, admin_path(conn, :create), admin: @valid_attrs
      body = json_response(conn, 201)
      assert body["data"]["id"]
      assert body["data"]["email"]
      refute body["data"]["password"]
      assert Repo.get_by(Admin, email: "foo@bar.com")
    end
  
    test "does not create resource and renders errors when data is invalid", %{conn: conn} do
      conn = post conn, admin_path(conn, :create), admin: @invalid_attrs
      assert json_response(conn, 422)["errors"] != %{}
    end
  
  end
  </code></pre>
  
  <p>admin_controller.ex</p>
  
  <pre><code>defmodule MyApp.AdminController do
    use MyApp.Web, :controller
  
    alias MyApp.Admin
  
    plug :scrub_params, "admin" when action in [:create]
  
    def create(conn, %{"admin" =&gt; admin_params}) do
      changeset = Admin.registration_changeset(%Admin{}, admin_params)
  
      case Repo.insert(changeset) do
        {:ok, admin} -&gt;
          conn
          |&gt; put_status(:created)
          |&gt; render("show.json", admin: admin)
        {:error, changeset} -&gt;
          conn
          |&gt; put_status(:unprocessable_entity)
          |&gt; render(MyApp.ChangesetView, "error.json", changeset: changeset)
      end
    end
  end
  </code></pre>
  
  <p>And finally parts of my router.ex</p>
  
  <pre><code>defmodule MyApp.Router do
    # [...]
  
    pipeline :api do
      plug :accepts, ["json"]
    end
  
    scope "/api/v1", MyApp do
      pipe_through :api
  
      resources "/admins", AdminController, only: [:create]
    end
  
    # [...]
  end
  </code></pre>
  
  <p>Any help is greatly appreciated! Thanks</p>