我的Users表中的用户名和电子邮件都有标准唯一性约束。现在,我正在测试我的 UserController 在用户创建期间的重复,似乎我的模型上的 unique_constraint 在单元测试期间不起作用,但在手动测试中很好。我在这里做错了什么?
迁移:
defmodule MyApi.Repo.Migrations.CreateUser do
use Ecto.Migration
def change do
create table(:users) do
add :username, :string, null: false
add :email, :string, null: false
add :password, :string, null: false
add :first_name, :string, null: false
add :last_name, :string, null: false
timestamps
end
create unique_index(:users, [:username])
create unique_index(:users, [:email])
end
end
控制器:
defmodule MyApi.V1.UserController do
use MyApi.Web, :controller
alias MyApi.User
def create(conn, %{"data" => data}) do
user_attrs = JaSerializer.Params.to_attributes(data)
user_changeset = User.changeset(%User{}, user_attrs)
case Repo.insert(user_changeset) do
{:ok, user} ->
conn
|> put_status(201)
|> render(:show, data: user)
{:error, user_changeset} ->
conn
|> put_status(500)
|> render(:errors, data: user_changeset)
end
end
end
测试
...
test "User creation with non-unique username", %{conn: conn} do
user_changeset = User.changeset(%User{}, @valid_attrs)
Repo.insert(user_changeset)
conn = post(conn, v1_user_path(conn, :create), user_params(@valid_attrs))
response = json_response(conn, 500)
errors = response["errors"]
assert errors
assert Enum.count(errors) == 1
{:ok, error} = Enum.fetch(errors, 0)
assert error["title"] == "has already been taken"
refute Repo.get_by(User, username: "name1")
end
...
混合测试
1) test User creation with non-unique username (MyApi.V1.UserControllerTest)
test/controllers/v1/user_controller_test.exs:57
** (RuntimeError) expected response with status 500, got: 201, with body:
{"jsonapi":{"version":"1.0"},"data":{"type":"user","id":"269","attributes":{"username":"name1","last-name":"lastname1","first-name":"first","email":"first@email.com"}}}
stacktrace:
(phoenix) lib/phoenix/test/conn_test.ex:362: Phoenix.ConnTest.response/2
(phoenix) lib/phoenix/test/conn_test.ex:408: Phoenix.ConnTest.json_response/2
test/controllers/v1/user_controller_test.exs:61: (test)
答案 0 :(得分:2)
如果您在创建迁移后运行mix test
,然后编辑了相同的迁移以添加unique_index
调用,则您的数据库将不会自动迁移到同一迁移的新版本。
有两种解决方案:
创建单独的迁移,将unique_index
添加到表格中。
下次运行mix test
时,系统会自动迁移您的测试数据库。
手动删除/创建或回滚/迁移(如果这是您最新的迁移)测试数据库:
或者:
MIX_ENV=test mix do ecto.drop, ecto.create, ecto.migrate
或(如果这是您最近的迁移):
MIX_ENV=test mix do ecto.rollback, ecto.migrate