Class Fixture

Namespace: AutoFixture
Assembly: AutoFixture.dll

Provides object creation services.

public class Fixture : IFixture, ISpecimenBuilder, IEnumerable<ISpecimenBuilder>, IEnumerable

Inheritance

objectFixture

Implements

IFixture, ISpecimenBuilder, IEnumerable<ISpecimenBuilder>, IEnumerable

Inherited Members

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

Extension Methods

CollectionFiller.AddManyTo<T>(IFixture, ICollection<T>), CollectionFiller.AddManyTo<T>(IFixture, ICollection<T>, int), CollectionFiller.AddManyTo<T>(IFixture, ICollection<T>, Func<T>), SpecimenFactory.Create<T>(ISpecimenBuilder), SpecimenFactory.CreateAnonymous<T>(IFixture), SpecimenFactory.CreateMany<T>(ISpecimenBuilder), SpecimenFactory.CreateMany<T>(ISpecimenBuilder, int), SpecimenCommand.Do<T>(ISpecimenBuilder, Action<T>), SpecimenCommand.Do<T1, T2>(ISpecimenBuilder, Action<T1, T2>), SpecimenCommand.Do<T1, T2, T3>(ISpecimenBuilder, Action<T1, T2, T3>), SpecimenCommand.Do<T1, T2, T3, T4>(ISpecimenBuilder, Action<T1, T2, T3, T4>), FixtureFreezer.Freeze<T>(IFixture), FixtureFreezer.Freeze<T>(IFixture, Func<ICustomizationComposer<T>, ISpecimenBuilder>), SpecimenQuery.Get<T, TResult>(ISpecimenBuilder, Func<T, TResult>), SpecimenQuery.Get<T1, T2, TResult>(ISpecimenBuilder, Func<T1, T2, TResult>), SpecimenQuery.Get<T1, T2, T3, TResult>(ISpecimenBuilder, Func<T1, T2, T3, TResult>), SpecimenQuery.Get<T1, T2, T3, T4, TResult>(ISpecimenBuilder, Func<T1, T2, T3, T4, TResult>), FixtureRegistrar.Inject<T>(IFixture, T), 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>), FixtureRepeater.Repeat<T>(IFixture, Func<T>), CustomizationExtensions.ToCustomization(ISpecimenBuilder)

Constructors

Fixture()

Initializes a new instance of the Fixture class.

public Fixture()

Fixture(DefaultRelays)

Initializes a new instance of the Fixture class with the supplied engine parts.

public Fixture(DefaultRelays engineParts)

Parameters

engineParts DefaultRelays

Fixture(ISpecimenBuilder, MultipleRelay)

Initializes a new instance of the Fixture class with the supplied engine and a definition of what 'many' means.

public Fixture(ISpecimenBuilder engine, MultipleRelay multiple)

Parameters

engine ISpecimenBuilder

The engine.

multiple MultipleRelay

The definition and implementation of 'many'.

Properties

Behaviors

Gets the behaviors that are applied as Decorators around other parts of a Fixture. is invoked.

public IList<ISpecimenBuilderTransformation> Behaviors { get; }

Property Value

IList<ISpecimenBuilderTransformation>

Customizations

Gets the customizations that intercept the request handling by the core.

public IList<ISpecimenBuilder> Customizations { get; }

Property Value

IList<ISpecimenBuilder>

Remarks

Any `ISpecimenBuilder` in this list are invoked before predefined builders giving them a chance to intercept a request and resolve it.

`ISpecimenBuilder%7d)` places resulting customizations in this list.

See Also

IFixture.Behaviors, IFixture.ResidueCollectors

Engine

Gets the core engine of the Fixture instance.

public ISpecimenBuilder Engine { get; }

Property Value

ISpecimenBuilder

Remarks

This is the core engine that drives a `Fixture` instance. Even with no `Customizations` or `ResidueCollectors`, the `Engine` should be capably of resolving a wide range of different requests, based on conventions.

OmitAutoProperties

Gets or sets if writable properties should generally be assigned a value when generating an anonymous object.

public bool OmitAutoProperties { get; set; }

Property Value

bool

RepeatCount

Gets or sets a number that controls how many objects are created when a Fixture creates more than one anonymous objects.

public int RepeatCount { get; set; }

Property Value

int

See Also

CollectionFiller.AddManyTo<T>(IFixture, ICollection<T>), CollectionFiller.AddManyTo<T>(IFixture, ICollection<T>, Func<T>), FixtureRepeater.Repeat<T>(IFixture, Func<T>)

ResidueCollectors

Gets the residue collectors that can be used to handle requests that neither the Customizations nor the predefined builders could handle.

public IList<ISpecimenBuilder> ResidueCollectors { get; }

Property Value

IList<ISpecimenBuilder>

Remarks

These `ISpecimenBuilder` instances will be invoked if no previous builder could resolve a request. This gives you the opportunity to define fallback strategies to deal with unresolved requests.

Methods

Build<T>()

Customizes the creation algorithm for a single object, effectively turning off all Customizations on the IFixture.

public ICustomizationComposer<T> Build<T>()

Returns

ICustomizationComposer<T>

A ICustomizationComposer%601 that can be used to customize the creation algorithm before creating the object.

Type Parameters

T

The type of object for which the algorithm should be customized.

Remarks

The Build method kicks off a Fluent API which is usually completed by invoking `IPostprocessComposer%7b%60%600%7d)` on the method chain.

Note that the Build method chain is best understood as a one-off Customization. It bypasses all Customizations on the `Fixture` instance. Instead, it allows fine-grained control when building a specific specimen. However, in most cases, adding a convention-based `ICustomization` is a better, more flexible option.

Create(object, ISpecimenContext)

Creates a new specimen based on a request.

public object Create(object request, ISpecimenContext context)

Parameters

request object

The request that describes what to create.

context ISpecimenContext

A context that can be used to create other specimens.

Returns

object

The requested specimen if possible; otherwise a NoSpecimen instance.

Remarks

The request can be any object, but will often be a `Type` or other `MemberInfo` instances.

Note to implementers: Implementations are expected to return a `NoSpecimen` instance if they can't satisfy the request.

Customize(ICustomization)

Applies a customization.

public IFixture Customize(ICustomization customization)

Parameters

customization ICustomization

The customization to apply.

Returns

IFixture

An IFixture where the customization is applied.

Remarks

Note to implementers: the returned `IFixture` is expected to have customization applied. Whether the return value is the same instance as the current instance, or a copy is unspecified.

Customize<T>(Func<ICustomizationComposer<T>, ISpecimenBuilder>)

Customizes the creation algorithm for all objects of a given type.

public void Customize<T>(Func<ICustomizationComposer<T>, ISpecimenBuilder> composerTransformation)

Parameters

composerTransformation Func<ICustomizationComposer<T>, ISpecimenBuilder>

A function that customizes a given ICustomizationComposer%601 and returns the modified composer.

Type Parameters

T

The type of object to customize.

Remarks

The resulting `ISpecimenBuilder` is added to `Customizations`.

GetEnumerator()

Gets an enumerator over the internal specimen builders used to create objects.

public IEnumerator<ISpecimenBuilder> GetEnumerator()

Returns

IEnumerator<ISpecimenBuilder>

An enumerator over the internal specimen builders used to create objects.