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

Was this helpful?

  1. AutoFixture

Relays

AutoFixture is focused on creating instances of concrete types. For this reason it doesn't have any support for non-concrete types like interfaces and abstract classes.

By default, AutoFixture is only able to work with concrete types. In some cases, interfaces and abstract classes have an implementation or a subtype that can be used as default. To handle cases like this, AutoFixture offers the class TypeRelay.

A TypeRelay is a customization that can be used to instruct the Fixture to relay requests of a certain type to another one.

public abstract class Animal { }

public class Dog : Animal { }

[Test]
public void Fixture_should_return_relayed_type()
{
    // ARRANGE
    var fixture = new Fixture();
    fixture.Customizations.Add(new TypeRelay(typeof(Animal), typeof(Dog)));

    // ACT
    var animal = fixture.Create<Animal>();

    // ASSERT
    Assert.That(animal, Is.InstanceOf<Dog>());
}
PreviousBuilding custom typesNextTricks

Last updated 4 years ago

Was this helpful?

AutoFixture uses relays to support well-known interfaces like IList<T>. The are forwarded to their most common implementation.

collection interfaces