ExMachina.Ecto
Summary
Gets a factory from the passed in attrs, or builds if none is present
Builds a factory with the passed in factory_name and returns its fields
Saves a record and all associated records using Repo.insert!
Functions
Gets a factory from the passed in attrs, or builds if none is present
Examples
attrs = %{user: %{name: "Someone"}}
# Returns attrs.user
assoc(attrs, :user)
attrs = %{}
# Builds and returns new instance based on :user factory
assoc(attrs, :user)
attrs = %{}
# Builds and returns new instance based on :user factory
assoc(attrs, :author, factory: :user)
Builds a factory with the passed in factory_name and returns its fields
This is only for use with Ecto models.
Will return a map with the fields and virtual fields, but without the Ecto metadata and associations.
Example
def factory(:user, _attrs) do
%MyApp.User{name: "John Doe", admin: false}
end
# Returns %{name: "John Doe", admin: true}
fields_for(:user, admin: true)
Saves a record and all associated records using Repo.insert!
Before inserting, changes are wrapped in a changeset. This means that has_many, has_one, embeds_one, and embeds_many associations will be saved correctly. Any belongs_to associations will also be saved.
# Will save the article and list of comments
create(:article, comments: [build(:comment)])