我创建了一个新的控制器,phoenix自动为它做了一些测试。
测试/控制器/ video_controller_test.exs
但是当我运行它们时,所有这些都会因重定向错误而崩溃,这就是一个例子:
1) test shows chosen resource (Rumbl.VideoControllerTest)
test/controllers/video_controller_test.exs:29
** (RuntimeError) expected response with status 200, got: 302, with body:
<html><body>You are being <a href="/">redirected</a>.</body></html>
code: assert html_response(conn, 200) =~ "Show video"
stacktrace:
(phoenix) lib/phoenix/test/conn_test.ex:362: Phoenix.ConnTest.response/2
(phoenix) lib/phoenix/test/conn_test.ex:376: Phoenix.ConnTest.html_response/2
test/controllers/video_controller_test.exs:32: (test)
warning: `Ecto.Changeset.cast/4` is deprecated, please use `cast/3` + `validate_required/3` instead
(rumbl) web/models/video.ex:19: Rumbl.Video.changeset/2
test/models/video_test.exs:10: Rumbl.VideoTest."test changeset with valid attributes"/1
(ex_unit) lib/ex_unit/runner.ex:292: ExUnit.Runner.exec_test/1
(stdlib) timer.erl:166: :timer.tc/1
(ex_unit) lib/ex_unit/runner.ex:240: anonymous fn/3 in ExUnit.Runner.spawn_test/3
warning: `Ecto.Changeset.cast/4` is deprecated, please use `cast/3` + `validate_required/3` instead
(rumbl) web/models/video.ex:19: Rumbl.Video.changeset/2
test/models/video_test.exs:15: Rumbl.VideoTest."test changeset with invalid attributes"/1
(ex_unit) lib/ex_unit/runner.ex:292: ExUnit.Runner.exec_test/1
(stdlib) timer.erl:166: :timer.tc/1
(ex_unit) lib/ex_unit/runner.ex:240: anonymous fn/3 in ExUnit.Runner.spawn_test/3
2) test updates chosen resource and redirects when data is valid (Rumbl.VideoControllerTest)
test/controllers/video_controller_test.exs:47
Assertion with == failed
code: assert redirected_to(conn) == video_path(conn, :show, video)
left: "/"
right: "/manage/videos/12"
stacktrace:
test/controllers/video_controller_test.exs:50: (test)
3) test lists all entries on index (Rumbl.VideoControllerTest)
test/controllers/video_controller_test.exs:8
** (RuntimeError) expected response with status 200, got: 302, with body:
<html><body>You are being <a href="/">redirected</a>.</body></html>
code: assert html_response(conn, 200) =~ "Listing videos"
stacktrace:
(phoenix) lib/phoenix/test/conn_test.ex:362: Phoenix.ConnTest.response/2
(phoenix) lib/phoenix/test/conn_test.ex:376: Phoenix.ConnTest.html_response/2
test/controllers/video_controller_test.exs:10: (test)
以下是这些测试的一些代码:
test "lists all entries on index", %{conn: conn} do
conn = get conn, video_path(conn, :index)
assert html_response(conn, 200) =~ "Listing videos"
end
test "renders form for new resources", %{conn: conn} do
conn = get conn, video_path(conn, :new)
assert html_response(conn, 200) =~ "New video"
end
test "creates resource and redirects when data is valid", %{conn: conn} do
conn = post conn, video_path(conn, :create), video: @valid_attrs
assert redirected_to(conn) == video_path(conn, :index)
assert Repo.get_by(Video, @valid_attrs)
end
test "does not create resource and renders errors when data is invalid", %{conn: conn} do
conn = post conn, video_path(conn, :create), video: @invalid_attrs
assert html_response(conn, 200) =~ "New video"
end
以下是控制器的一些代码:
def action(conn, _) do
apply(__MODULE__, action_name(conn),
[conn, conn.params, conn.assigns.current_user])
end
def index(conn, _params, user) do
videos = Repo.all(user_videos(user)) |> Repo.preload(:category)
render(conn, "index.html", videos: videos)
end
def new(conn, _params, user) do
changeset =
user
|> build_assoc(:videos)
|> Video.changeset()
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"video" => video_params}, user) do
changeset =
user
|> build_assoc(:videos)
|> Video.changeset(video_params)
case Repo.insert(changeset) do
{:ok, _video} ->
conn
|> put_flash(:info, "Video created successfully.")
|> redirect(to: video_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
我必须错过一些显而易见的东西但不能让测试工作