I have a basic user schema that contains password_digest to be saved in db and virtual fields password that user will input, hashed, saved as password_digest.
The problem is when testing, I am getting password as nil the other hand I have them as non-nil. I am not sure what the best practice is when testing virtual fields.
...
Assertion with == failed
code: assert Accounts.get_user!(user.id()) == user
left: ... username: "some username", password: nil}
right: ... username: "some username", password: "some password"}
schema "users" do
field :email, :string
field :password_digest, :string
field :username, :string
timestamps()
field :password, :string, virtual: true
end
Here is the relevant codes:
def changeset(user, attrs) do
user
|> cast(attrs, [:username, :email, :password])
|> hash_password
|> validate_required([:username, :email, :password, :password_digest])
end
defp hash_password(changeset) do
if password = get_change(changeset, :password) do
changeset
|> put_change(:password_digest, hashpwsalt(password))
else
changeset
end
end
Inside the test, I have these:
@valid_attrs %{email: "some email", password: "some password", username: "some username"}
def user_fixture(attrs \\ %{}) do
{:ok, user} =
attrs
|> Enum.into(@valid_attrs)
|> Accounts.create_user()
user
end
...
test "get_user!/1 returns the user with given id" do
user = user_fixture()
assert Accounts.get_user!(user.id) == user
end
Btw, get_user is straight from generator: def get_user!(id), do: Repo.get!(User, id)
What is the proper way to test virtual field?
Aucun commentaire:
Enregistrer un commentaire