ExMachina.Ecto

Summary

Builds a factory with the passed in factory_name and returns its fields

Saves a record and all associated records using Repo.insert!

Functions

fields_for(module, factory_name, attrs \\ %{})

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) do
  %MyApp.User{name: "John Doe", admin: false}
end

# Returns %{name: "John Doe", admin: true}
fields_for(:user, admin: true)
save_record(module, repo, record)

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)])