Building custom types
Last updated
Last updated
Creating instances of custom types is where AutoFixture really shines.
By mixing the and reflection, AutoFixture is able to generate instances of almost every type, even without an explicit configuration.
When analyzing an unknown type, AutoFixture will use reflection to find the easiest way to create an instance of the type.
First, it looks for a public constructor, if many constructors are found, the one with fewest parameters are selected. If any parameter is needed, AutoFixture will recursively create instances of the needed types to satisfy the requirements of the selected constructor. If the constructor can't be invoked because its parameters can't be instantiated, the constructors are discarded.
If no suitable public constructor is found, AutoFixture will look for a static method returning an instance of the type. If multiple methods are found, the one with fewest argument is selected. Like for constructors, AutoFixture will take care of creating instances to be fed as parameters to the selected static method.
If no static method matching the requirements is found, an exception is thrown.
Once the object is constructed or obtained from a static factory method, all writeable properties are provided with a value.
Here are some examples on how custom types are built
In the sample below, the parameterless constructor is selected
Here, the constructor with fewer arguments is selected
Finally, the static factory method with fewer arguments is used to instantiate the type.