ecto_morph icon indicating copy to clipboard operation
ecto_morph copied to clipboard

Add a "Factories" section in the Readme

Open Adzz opened this issue 3 years ago • 0 comments

Using ecto_morph as a factory in tests allows us to leverage changesets to create valid data structures. We should demo how this can be nice for factory functions:

def build_user(attrs) do
  defaults = %{email: "[email protected]"}
  Map.merge(default, attrs)
  |> EctoMorph.cast_to_struct!(User)
end

def insert_user(attrs) do
  defaults = %{email: "[email protected]"}
  Map.merge(default, attrs)
  |> EctoMorph.cast_to_struct!(User)
  |> Db.Repo.insert
end

Demo with nested data - you just pass a map in.

Adzz avatar Apr 06 '21 13:04 Adzz