ExZample v0.2.0 ExZample behaviour View Source

ExZample is a factory library based on Elixir behaviours.

Link to this section Summary

Functions

Builds a struct with given factory module.

Same as build/2, but returns a list with where the size is the given count.

Same as build/2, but returns a tuple with a pair of structs.

Callbacks

Invoked every time you build your data using ExZample module.

Link to this section Types

Link to this section Functions

Link to this function

build(factory, attrs \\ nil)

View Source
build(factory(), Enum.t() | nil) :: struct()

Builds a struct with given factory module.

If the given factory exports the example/0 function it will use to return the struct and its values. Otherwise, if the module is a struct it will use its default values.

If will override the generated data with the given attrs.

Examples

iex> ExZample.build(User)
%ExZample.User{}

iex> ExZample.build(Factories.User)
%ExZample.User{age: 21, email: "test@test.test", first_name: "First Name", id: 1, last_name: "Last Name"}

iex> ExZample.build(User, age: 45)
%ExZample.User{age: 45}

iex> ExZample.build(Factories.User, age: 45)
%ExZample.User{age: 45, email: "test@test.test", first_name: "First Name", id: 1, last_name: "Last Name"}
Link to this function

build_list(count, factory, attrs \\ nil)

View Source
build_list(count :: pos_integer(), factory(), attrs :: Enum.t() | nil) :: [
  struct()
]

Same as build/2, but returns a list with where the size is the given count.

Examples

iex> ExZample.build_list(3, User)
[%ExZample.User{}, %ExZample.User{}, %ExZample.User{}]

iex> ExZample.build_list(3, User, age: 45)
[%ExZample.User{age: 45}, %ExZample.User{age: 45}, %ExZample.User{age: 45}]
Link to this function

build_pair(factory, attrs \\ nil)

View Source
build_pair(factory(), attrs :: Enum.t() | nil) :: {struct(), struct()}

Same as build/2, but returns a tuple with a pair of structs.

Examples

iex> ExZample.build_pair(User)
{%ExZample.User{}, %ExZample.User{}}

iex> ExZample.build_pair(User, age: 45)
{%ExZample.User{age: 45}, %ExZample.User{age: 45}}

Link to this section Callbacks

Link to this callback

example()

View Source (optional)
example() :: struct()

Invoked every time you build your data using ExZample module.

You need to return a struct with example values.

This callback is optional when the module given is a struct. It will use the struct default values if no callback is given.