ex_machina v1.0.0 ExMachina.Ecto
Module for building and inserting factories with Ecto
This module works much like the regular ExMachina
module, but adds a few
nice things that make working with Ecto easier.
- It uses
ExMachina.EctoStrategy
, which addsinsert/1
,insert/2
,insert_pair/2
,insert_list/3
. - Adds a
params_for
function that is useful for working with changesets or sending params to API endpoints.
More in-depth examples are in the README.
Summary
Functions
Builds a factory with the passed in factory_name and returns its fields
Same as params_for/2
, but inserts all belongs_to associations and sets the
foreign keys
Functions
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, associations, and the primary key.
If you want belongs_to associations to be inserted, use
params_with_assocs/2
.
Example
def user_factory do
%MyApp.User{name: "John Doe", admin: false}
end
# Returns %{name: "John Doe", admin: true}
params_for(:user, admin: true)
Same as params_for/2
, but inserts all belongs_to associations and sets the
foreign keys.
Example
def article_factory do
%MyApp.Article{title: "An Awesome Article", author: build(:author)}
end
# Inserts an author and returns %{title: "An Awesome Article", author_id: 12}
params_with_assocs(:article)