Create and Build
AutoFixture gives its users the ability to quickly create anonymous variables or to customize how they are created, totally or partially.
The snippets below will be based on the following custom types
Create
Create
The Create
method is responsible for initiating the construction of the requested type. When invoked, AutoFixture will use all current customizations and default configurations.
AutoFixture supports also complex types, even when not part of the BCL.
Build
Build
Before creating the object, the Build method can be used to add one-time customizations to be used for the creation of the next variable. Once the object is created, the customizations are lost
As shown in the snippet, Build
is used to initiate the fluent Customization API. This API is composed by many methods.
OmitAutoProperties
OmitAutoProperties
OmitAutoProperties
disables the assignment of values to properties.
WithAutoProperties
WithAutoProperties
Opposite to OmitAutoProperties
, WithAutoProperties
forces the assignment of values to properties, even if it had been disabled on the fixture
With
With
The With
construct allows the customization of writeable properties and public fields.
There are different overloads, each with its own use case and semantic.
No value specified
This overload is used to signal AutoFixture that the selected property should receive a value even if property generation was disabled earlier for the whole type with OmitAutoProperties()
or by setting the OmitAutoProperties
property of the fixture to false
.
With a specified value
This overload is used to assign a specific value to the selected property.
With a factory method
This overload is used to provide a lazily-evaluated factory method used to generate the value of the property. The factory method can have either no or one parameter. If requested, AutoFixture will create and provide an instance of the requested argument type.
If more than one argument is needed, developers can request for a tuple composed by the needed types.
Finally, developers can request for an instance of IFixture
. If so, the same instance will be passed.
Without
Without
The Without
construct can be used to disable the generation of the value for a specific property without affecting other properties.
Please notice the difference between Without
and With(null)
: the first skips the selected properties (leaving the default value) whilst the second assigns it a null
.
Also, since delegates can be null too, developers need to cast null to the type of the property to help the compiler picking the right overload.
FromFactory
FromFactory
Some types are too complex to build for AutoFixture to guess. In these cases developers can use the FromFactory
construct to instruct AutoFixture.
Like for the With
construct, FromFactory
allows the developer to specify a function delegate that will be used to generate the object. There are overloads who accept from 0 to 4 arguments. AutoFixture will provide a value for each requested argument.
On top of these overloads, FromFactory
has an overload accepting an ISpecimenBuilder
for more advanced scenarios.
Do
Do
If the value to be built requires some methods to be invoked as part of its creation, you can use the Do
construct to instruct AutoFixture to do so.
You can find additional information about Do
on this blog post from the author of AutoFixture
Last updated