Documentation ¶
Overview ¶
gory is a fixtures replacement with a straightforward definition syntax. It's to Go what thoughtbot/factory_girl is to Ruby.
Index ¶
- func Build(definitionName string) interface{}
- func BuildWithParams(definitionName string, params Factory) interface{}
- func Define(definitionName string, instance interface{}, builder FactoryBuilder)
- func Lazy(callback func() interface{}) next
- func Sequence(sequencer Sequencer) next
- type Factory
- type FactoryBuilder
- type Sequencer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Build ¶
func Build(definitionName string) interface{}
Returns an instance of the struct defined using the definitionName parameter and the Define() function. If no matching definition exists, this function panics. It also panics if an attempt to set an invalid field was made from within the Define() function.
Example:
person := gory.Build("person").(*Person) fmt.Println(person.FirstName) // "Jane"
func BuildWithParams ¶
Returns an instance of the struct defined by definitionName, but using the specified parameters instead of the defined factory's values. If names of fields that don't exist or are not exported are specified, the function panics.
Example:
person := gory.BuildWithParams("person", Person{}, { "Name": "John" }).(*Person) fmt.Println(person.Name) // "John" instead of Factory value
func Define ¶
func Define(definitionName string, instance interface{}, builder FactoryBuilder)
Define a factory. Factories can be retrieved later by using the definitionName parameter. You cannot define two factories with identical names.
The instance parameter must be a struct literal of the type you'd like the factory to return when building.
The builder parameter is a function executed when defining factories. Use the factory parameter as a map to store values for your struct fields. If this is nil, no fields on the returned struct are set.
func Lazy ¶
func Lazy(callback func() interface{}) next
Used to set a lazily evaluated value on a Factory.
Example:
factory["Created"] = gory.Lazy(func() interface{} { return time.Now() })
In the above example, time.Now() is not evaluated until the Factory is built, using the Build() function.
func Sequence ¶
func Sequence(sequencer Sequencer) next
Used to set an sequenced value on a Factory.
Example:
factory["Email"] = gory.Sequence(func(n int) interface{} { return fmt.Sprintf("john-doe-%d@example.com", n) })
Each time Build() is called, the sequenced value will use an incremented value for n.
Types ¶
type FactoryBuilder ¶
type FactoryBuilder func(factory Factory)
A function executed when defining factories. Use the factory parameter as a map to store values for your struct fields.
Example:
gory.Define("person", Person{}, func(factory gory.Factory) { factory["FirstName"] = "Jane" factory["LastName"] = "Doe" factory["Admin"] = false })
Later, when you build the object, the FirstName, LastName, and Admin fields will be set to the parameters you specified. If you attempt to set fields that don't exist or are not exported, the Build() function will panic once called.