Unit Testing in C#
  • Unit testing in C#
  • Unit testing
    • What to test
    • When to test
    • Qualities of a good unit test suite
    • Qualities of a good unit test
    • Dealing with dependencies
    • Running the tests
  • NUnit
    • Quick glance at NUnit
    • Creating a NUnit test project
    • Anatomy of a test fixture
    • Lifecycle of a test fixture
    • Assertions
    • Asynchronous executions
    • Parameterized tests
    • Assumptions
    • Describing your tests
  • Moq
    • Quick glance at Moq
    • Method arguments
    • Method calls
    • Properties
    • Results
    • Callbacks
    • Exceptions
    • Events
    • Verifications
    • Base class
    • Mock customization
    • Implicit mocks
    • Mock repository
    • Custom matchers
    • Multiple interfaces
    • Protected members
    • Generic methods
    • Delegates
  • AutoFixture
    • Quick glance at AutoFixture
    • Fixture
    • Create and Build
    • Type customization
    • Data annotations
    • Default configurations
    • Building custom types
    • Relays
    • Tricks
    • Idioms
    • Integration with NUnit
    • Integration with Moq
    • Combining AutoFixture with NUnit and Moq
    • Extending AutoFixture
  • Advanced topics
    • Testing HttpClient
Powered by GitBook
On this page
  • How custom types are created
  • Some examples
  1. AutoFixture

Building custom types

PreviousDefault configurationsNextRelays

Last updated 4 years ago

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.

var fixture = new Fixture();
var person = fixture.Create<Person>();

Assert.That(person, Is.Not.Null);
Assert.That(person.FirstName, Is.Not.Null);
Assert.That(person.LastName, Is.Not.Null);

How custom types are created

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.

Some examples

Here are some examples on how custom types are built

In the sample below, the parameterless constructor is selected

public class TestClass
{
    public TestClass () {}

    public TestClass (int value) { }
}

Here, the constructor with fewer arguments is selected

public class TestClass
{
    public TestClass(int value) { }

    public TestClass(int value, string anotherValue) { }
}

Finally, the static factory method with fewer arguments is used to instantiate the type.

public class TestClass
{
    private TestClass () {}

    public static TestClass Create(int value) => new TestClass();

    public static TestClass Create(int value, string anotherValue) => new TestClass();
}
default configurations