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.
The error message in question (when running the tests).
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
My Code:
admin_controller_test.exs
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
admin_controller.ex
defmodule MyApp.AdminController do
use MyApp.Web, :controller
alias MyApp.Admin
plug :scrub_params, "admin" when action in [:create]
def create(conn, %{"admin" => admin_params}) do
changeset = Admin.registration_changeset(%Admin{}, admin_params)
case Repo.insert(changeset) do
{:ok, admin} ->
conn
|> put_status(:created)
|> render("show.json", admin: admin)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(MyApp.ChangesetView, "error.json", changeset: changeset)
end
end
end
And finally parts of my router.ex
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
Any help is greatly appreciated! Thanks
The error is coming from:
setup %{conn: conn} do
Change this to:
setup do
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.