Type customization
The same fluent API used to specify how to build an anonymous variable can be used to instruct AutoFixture how to create every instance of the same type.
This can be done using the Customize<T>
method of IFixture
.
These type-wide customizations can be overridden by customizing the object creation via Build<T>
. In this case, the customization is totally replaced.
Register
Register
Certain customizations are so common that AutoFixture offers shortcuts to them.
The Register
extension method can be used as replacement for a customization composed by a call to FromFactory
followed by OmitAutoProperties
.
In the snippet above, the two commands are equivalent.
Register
can be used to handle custom interfaces since they are not natively supported by AutoFixture.
This approach works pretty well with simple scenarios (with simple or no dependencies). To handle more complex scenarios, it's better to use relays.
Finally, the generator method can also return a subtype of the registered type.
Inject
Inject
Another common scenario is customizing a type so that the same instance is returned at every request.
The Inject
extension method can be used as a replacement for a call to Register
that uses an instance already existing.
The snippet above can be replaced with
Like Register
, Inject
can accept a subtype of the type being configured.
Finally, Inject
is the best way to force a value to an Enum
.
Freeze
Freeze
Given how common the Create/Inject combination is, AutoFixture offers a shortcut.
The Freeze
method creates an anonymous variable, injects it and returns it to the caller.
In the snippet above, the two commands are equivalent.
To be noted that Freeze
returns the frozen instance. We will see how this will be useful when integrating AutoFixture with NUnit.
Last updated