Why test does not pass?

Hi all
I have following test and do not know, why it failed:

defmodule Rumbl.UserRepoTest do
  use Rumbl.ModelCase
  alias Rumbl.User

  @valid_attrs %{name: "A User", username: "eva"}

  test "converts unique_constraint on username to error" do
    insert_user(username: "eric")
    attrs = Map.put(@valid_attrs, :username, "eric")
    changeset = User.changeset(%User{}, attrs)

    assert {:error, changeset} = Repo.insert(changeset)
    assert {:username, "has already been taken"} in changeset.errors

The message, why it failed

Developers-MacBook-Pro:rumbl developer$ mix test test/models/user_repo_test.exs

  1) test converts unique_constraint on username to error (Rumbl.UserRepoTest)
     Assertion with in failed
     code: {:username, "has already been taken"} in changeset.errors()
     lhs:  {:username, "has already been taken"}
     rhs:  [username: {"has already been taken", []}]
       test/models/user_repo_test.exs:13: (test)

What is wrong?


From the changelog for Ecto v2:

[Changeset] changeset.errors now always returns tuple {String.t, Keyword.t} in its values

All error messages are tuples. Your code is trying to match the string "has already been taken" to the tuple {"has already been taken", []}.

1 Like

If you haven’t yet gotten this test to pass, I ran into the same problem and rewrote the assert as
assert {:username, {"has already been taken", []}} in changeset.errors and my test passed.

I’m a beginner to Elixir, so if anyone has a better solution, let me know!

Jon Miller

1 Like