Changeset View
Changeset View
Standalone View
Standalone View
larus/test/support/model_case.ex
Show All 35 Lines | defmodule Larus.ModelCase do | ||||
@doc """ | @doc """ | ||||
Helper for returning list of errors in model when passed certain data. | Helper for returning list of errors in model when passed certain data. | ||||
## Examples | ## Examples | ||||
Given a User model that lists `:name` as a required field and validates | Given a User model that lists `:name` as a required field and validates | ||||
`:password` to be safe, it would return: | `:password` to be safe, it would return: | ||||
iex> errors_on(%User{}, %{password: "password"}) | iex> errors_on(%User{}, %{password: "pass"}) | ||||
[password: "is unsafe", name: "is blank"] | [password: "is unsafe", name: "is blank"] | ||||
You could then write your assertion like: | You could then write your assertion like: | ||||
assert {:password, "is unsafe"} in errors_on(%User{}, %{password: "password"}) | assert {:password, "is unsafe"} in errors_on(%User{}, %{password: "pass"}) | ||||
You can also create the changeset manually and retrieve the errors | You can also create the changeset manually and retrieve the errors | ||||
field directly: | field directly: | ||||
iex> changeset = User.changeset(%User{}, password: "password") | iex> changeset = User.changeset(%User{}, password: "password") | ||||
iex> {:password, "is unsafe"} in changeset.errors | iex> {:password, "is unsafe"} in changeset.errors | ||||
true | true | ||||
""" | """ | ||||
def errors_on(model, data) do | def errors_on(model, data) do | ||||
model.__struct__.changeset(model, data).errors | model.__struct__.changeset(model, data).errors | ||||
end | end | ||||
end | end |