code_corps v0.0.1 CodeCorps.ModelCase View Source
This module defines the test case to be used by model tests.
You may define functions here to be used as helpers in
your model tests. See errors_on/2’s definition as reference.
Finally, if the test case interacts with the database, it cannot be async. For this reason, every test runs inside a transaction which is reset at the beginning of the test unless the test case is marked as async.
Link to this section Summary
Functions
Asserts if a specific error message has been added to a specific field on the
changeset. It is more flexible to use error_message/2 directly instead of
this one
Asserts if a specific validation type has been triggered on a specific field
on the changeset. It is more flexible to use validation_triggered/2 directly
instead of this one
Returns true or false depending on if an assoc_constraint validation has been triggered in the provided changeset on the specified field
Returns an error message on a specific field on the specified changeset
Helper for returning list of errors in a struct when given certain data
Returns an atom indicating the type of validation that was triggered on a field in a changeset
Link to this section Functions
Asserts if a specific error message has been added to a specific field on the
changeset. It is more flexible to use error_message/2 directly instead of
this one.
assert_error_message(changeset, :foo, "bar")
Compared to
assert error_message(changeset, :foo) == "bar"
refute error_message?(changeset, :foo) == "baz"
Asserts if a specific validation type has been triggered on a specific field
on the changeset. It is more flexible to use validation_triggered/2 directly
instead of this one.
assert_validation_triggered(changeset, :foo, "bar")
Compared to
assert validation_triggered(changeset, :foo) == :required
refute validation_triggered?(changeset, :bar) == :required
assoc_constraint_triggered?(Ecto.Changeset.t(), Atom.t()) :: boolean()
Returns true or false depending on if an assoc_constraint validation has been triggered in the provided changeset on the specified field.
error_message(Ecto.Changeset.t(), Atom.t()) :: String.t()
Returns an error message on a specific field on the specified changeset
Helper for returning list of errors in a struct when given certain data.
Examples
Given a User schema that lists :name as a required field and validates
:password to be safe, it would return:
iex> errors_on(%User{}, %{password: "password"})
[password: "is unsafe", name: "is blank"]
You could then write your assertion like:
assert {:password, "is unsafe"} in errors_on(%User{}, %{password: "password"})
You can also create the changeset manually and retrieve the errors field directly:
iex> changeset = User.changeset(%User{}, password: "password")
iex> {:password, "is unsafe"} in changeset.errors
true
validation_triggered(Ecto.Changeset.t(), Atom.t()) :: Atom.t()
Returns an atom indicating the type of validation that was triggered on a field in a changeset.