Class FixtureRegistrar

Namespace: AutoFixture
Assembly: AutoFixture.dll

Contains extension methods for registering specimens in IFixture instances.

public static class FixtureRegistrar

Inheritance

objectFixtureRegistrar

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Methods

Inject<T>(IFixture, T)

Injects a specific instance for a specific type, in order to make that instance a shared instance, no matter how many times the Fixture is asked to create an instance of that type.

public static void Inject<T>(this IFixture fixture, T item)

Parameters

fixture IFixture

The fixture.

item T

The item to inject.

Type Parameters

T

The type for which item should be injected.

Examples

This example demonstrates that when injecting an instance of the custom class MyClass into a Fixture instance, that Fixture instance will return the originally injected MyClass instance every time it's asked to create an instance of MyClass.

var fixture = new Fixture();
var original = new MyClass();
fixture.Inject(original);

var actual1 = fixture.Create<MyClass>();
var actual2 = fixture.Create<MyClass>();

// actual1 and actual2 are equal, and equal to original
Assert.Same(actual1, actual2);
Assert.Same(original, actual1);
Assert.Same(original, actual2);

Remarks

Injecting an instance of a specific type into a `Fixture` effectively 'locks' the type to that specific instance. The injected item becomes a shared instance. No matter how many times the Fixture instance is asked to create an instance of T, the shared item is returned.

It's possible to inject a sub-type of T into the Fixture. As long as the item can be converted to T (i.e. as long at the code compiles), you can inject a sub-type of T into the Fixture. This can for example be used to lock an interface to a specific instance of a concrete type.

If you are familiar with DI Container lifetime management, the following parallel may be helpful. If not, skip the next paragraph.

Most DI Containers come with several built-in lifetime styles. The two most common lifetime styles are Transient (a new instance is created for every request) and Singleton (the same instance is used for all requests) (don't confuse the Singleton lifetime style with the Singleton design pattern; they are related, but different). By default, Fixture uses the Transient lifetime style: it creates a new instance for every request. However, using the Inject method, effectively changes the lifetime style for that particular type to Singleton.

See Also

FixtureRegistrar.Register<T>(IFixture, Func<T>), FixtureRegistrar.Register<TInput, T>(IFixture, Func<TInput, T>), FixtureRegistrar.Register<TInput1, TInput2, T>(IFixture, Func<TInput1, TInput2, T>), FixtureRegistrar.Register<TInput1, TInput2, TInput3, T>(IFixture, Func<TInput1, TInput2, TInput3, T>), FixtureRegistrar.Register<TInput1, TInput2, TInput3, TInput4, T>(IFixture, Func<TInput1, TInput2, TInput3, TInput4, T>)

Register<T>(IFixture, Func<T>)

Registers a creation function for a specific type.

public static void Register<T>(this IFixture fixture, Func<T> creator)

Parameters

fixture IFixture

The fixture.

creator Func<T>

A function that will be used to create objects of type T every time the Fixture is asked to create an object of that type.

Type Parameters

T

The type for which creator should be registered.

Register<TInput, T>(IFixture, Func<TInput, T>)

Registers a creation function for a specific type, when that creation function requires a single input parameter.

public static void Register<TInput, T>(this IFixture fixture, Func<TInput, T> creator)

Parameters

fixture IFixture

The fixture.

creator Func<TInput, T>

A function that will be used to create objects of type T every time the Fixture is asked to create an object of that type.

Type Parameters

TInput

The type of the input parameter used by creator.

T

The type for which creator should be registered.

Register<TInput1, TInput2, T>(IFixture, Func<TInput1, TInput2, T>)

Registers a creation function for a specific type, when that creation function requires two input parameters.

public static void Register<TInput1, TInput2, T>(this IFixture fixture, Func<TInput1, TInput2, T> creator)

Parameters

fixture IFixture

The fixture.

creator Func<TInput1, TInput2, T>

A function that will be used to create objects of type T every time the Fixture is asked to create an object of that type.

Type Parameters

TInput1

The type of the first input parameter used by creator.

TInput2

The type of the second input parameter used by creator.

T

The type for which creator should be registered.

Register<TInput1, TInput2, TInput3, T>(IFixture, Func<TInput1, TInput2, TInput3, T>)

Registers a creation function for a specific type, when that creation function requires three input parameters.

public static void Register<TInput1, TInput2, TInput3, T>(this IFixture fixture, Func<TInput1, TInput2, TInput3, T> creator)

Parameters

fixture IFixture

The fixture.

creator Func<TInput1, TInput2, TInput3, T>

A function that will be used to create objects of type T every time the Fixture is asked to create an object of that type.

Type Parameters

TInput1

The type of the first input parameter used by creator.

TInput2

The type of the second input parameter used by creator.

TInput3

The type of the third input parameter used by creator.

T

The type for which creator should be registered.

Register<TInput1, TInput2, TInput3, TInput4, T>(IFixture, Func<TInput1, TInput2, TInput3, TInput4, T>)

Registers a creation function for a specific type, when that creation function requires four input parameters.

public static void Register<TInput1, TInput2, TInput3, TInput4, T>(this IFixture fixture, Func<TInput1, TInput2, TInput3, TInput4, T> creator)

Parameters

fixture IFixture

The fixture.

creator Func<TInput1, TInput2, TInput3, TInput4, T>

A function that will be used to create objects of type T every time the Fixture is asked to create an object of that type.

Type Parameters

TInput1

The type of the first input parameter used by creator.

TInput2

The type of the second input parameter used by creator.

TInput3

The type of the third input parameter used by creator.

TInput4

The type of the fourth input parameter used by creator.

T

The type for which creator should be registered.