Class FixtureRegistrar
Contains extension methods for registering specimens in IFixture instances.
Inheritance
Inherited Members
Namespace: AutoFixture
Assembly: AutoFixture.dll
Syntax
public static class FixtureRegistrar
Methods
| Improve this Doc View SourceInject<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.
Declaration
public static void Inject<T>(this IFixture fixture, T item)
Parameters
Type | Name | Description |
---|---|---|
IFixture | fixture | The fixture. |
T | item | The item to inject. |
Type Parameters
Name | Description |
---|---|
T | The type for which |
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.
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);
See Also
Register<T>(IFixture, Func<T>)
Registers a creation function for a specific type.
Declaration
public static void Register<T>(this IFixture fixture, Func<T> creator)
Parameters
Type | Name | Description |
---|---|---|
IFixture | fixture | The fixture. |
System.Func<T> | creator | A function that will be used to create objects of type |
Type Parameters
Name | Description |
---|---|
T | The type for which |
Register<TInput, T>(IFixture, Func<TInput, T>)
Registers a creation function for a specific type, when that creation function requires a single input parameter.
Declaration
public static void Register<TInput, T>(this IFixture fixture, Func<TInput, T> creator)
Parameters
Type | Name | Description |
---|---|---|
IFixture | fixture | The fixture. |
System.Func<TInput, T> | creator | A function that will be used to create objects of type |
Type Parameters
Name | Description |
---|---|
TInput | The type of the input parameter used by |
T | The type for which |
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.
Declaration
public static void Register<TInput1, TInput2, T>(this IFixture fixture, Func<TInput1, TInput2, T> creator)
Parameters
Type | Name | Description |
---|---|---|
IFixture | fixture | The fixture. |
System.Func<TInput1, TInput2, T> | creator | A function that will be used to create objects of type |
Type Parameters
Name | Description |
---|---|
TInput1 | The type of the first input parameter used by |
TInput2 | The type of the second input parameter used by |
T | The type for which |
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.
Declaration
public static void Register<TInput1, TInput2, TInput3, T>(this IFixture fixture, Func<TInput1, TInput2, TInput3, T> creator)
Parameters
Type | Name | Description |
---|---|---|
IFixture | fixture | The fixture. |
System.Func<TInput1, TInput2, TInput3, T> | creator | A function that will be used to create objects of type |
Type Parameters
Name | Description |
---|---|
TInput1 | The type of the first input parameter used by |
TInput2 | The type of the second input parameter used by |
TInput3 | The type of the third input parameter used by |
T | The type for which |
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.
Declaration
public static void Register<TInput1, TInput2, TInput3, TInput4, T>(this IFixture fixture, Func<TInput1, TInput2, TInput3, TInput4, T> creator)
Parameters
Type | Name | Description |
---|---|---|
IFixture | fixture | The fixture. |
System.Func<TInput1, TInput2, TInput3, TInput4, T> | creator | A function that will be used to create objects of type |
Type Parameters
Name | Description |
---|---|
TInput1 | The type of the first input parameter used by |
TInput2 | The type of the second input parameter used by |
TInput3 | The type of the third input parameter used by |
TInput4 | The type of the fourth input parameter used by |
T | The type for which |