I have some tests (pretty sure these were originally generated by phx.gen.auth) which attempt to violate DB constraints and assert that an error is raised. The tests pass, but they display all the errors in the test output. I’m wondering if this is working as intended, or if there is a better way to run the test.
Here is one test:
test "validates email uniqueness" do
%{email: email} = user_fixture()
attrs = %{username: "tester", password: "test1234", email: email}
{:error, changeset} = Accounts.register_user(attrs)
assert "has already been taken" in errors_on(changeset).email
end
which results in
2021-04-21 12:45:24.569 CDT [14410] ERROR: duplicate key value violates unique constraint "users_email_index"
2021-04-21 12:45:24.569 CDT [14410] DETAIL: Key (email)=(user-576460752303389599@example.com) already exists.
2021-04-21 12:45:24.569 CDT [14410] STATEMENT: INSERT INTO "users" ("email","hashed_password","username","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id"
being shown in the test output.
There is another test which uses a different approach - assert_raise/2
- which seems like it would do exactly what I want:
test "generates a token", %{user: user} do
token = Accounts.generate_user_session_token(user)
assert user_token = Repo.get_by(UserToken, token: token)
assert user_token.context == "session"
# Creating the same token for another user should fail
assert_raise Ecto.ConstraintError, fn ->
Repo.insert!(%UserToken{
token: user_token.token,
user_id: user_fixture().id,
context: "session"
})
end
end
but the result is basically the same:
2021-04-21 12:45:24.496 CDT [14408] ERROR: duplicate key value violates unique constraint "users_tokens_context_token_index"
2021-04-21 12:45:24.496 CDT [14408] DETAIL: Key (context, token)=(session, \x8a965dd47a14cbaf5e3befdffdae91a0834e63acc0785ed7363e8cdeddb01daf) already exists.
2021-04-21 12:45:24.496 CDT [14408] STATEMENT: INSERT INTO "users_tokens" ("context","token","user_id","inserted_at") VALUES ($1,$2,$3,$4) RETURNING "id"
Is there a better way to handle these error-case tests which will result in “clean” output, or should I just not worry about it since the tests are passing?