Skip to content

MockOf<T>

ClassMockMagiccom.wetzold.mock-magic
public sealed class MockOf<T>

Delegate-based fluent object builder with no reflection or expression trees — safe for Unity's IL2CPP/AOT backend.

public static MockOf<T> New(Func<T> factory)

Start a new builder using factory to allocate instances.

Parameters

NameType
factorySystem.Func{{T}}

Returns

MockMagic.MockOf`1

public static MockOf<T> New()

Start a new builder that allocates instances via T’s public parameterless constructor (Activator.CreateInstance<T>()). Convenience overload for plain POCOs; if your type has no public default constructor, use MockOf<T>.New(Func<T>) instead.

Returns

MockMagic.MockOf`1

public MockOf<T> Set(Action<T> setter)

Register a setter invoked for every generated instance.

Parameters

NameType
setterSystem.Action{{T}}

Returns

MockMagic.MockOf`1

public MockOf<T> Set(Action<T, int> setter)

Register a setter with access to the zero-based item index.

Parameters

NameType
setterSystem.Action{{T},System.Int32}

Returns

MockMagic.MockOf`1

public MockOf<T> Finish(Action<T> finisher)

Register a finishing hook invoked after all setters run.

Parameters

NameType
finisherSystem.Action{{T}}

Returns

MockMagic.MockOf`1

public MockOf<T> UseSeed(int seed)

Use a fixed seed for each generation pass. MockOf<T>.Generate(), MockOf<T>.Generate(int) and MockOf<T>.GenerateLazy(int) seed the shared generators for their own pass only and restore the previous random state, seed and unique-ID counter afterwards, so code outside the builder keeps its own sequence.

Parameters

NameType
seedSystem.Int32

Returns

MockMagic.MockOf`1

public MockOf<T> UseLocale(string id)

Capture a data locale for each generation operation; an unset builder inherits its caller’s scope.

Parameters

NameType
idSystem.String

Returns

MockMagic.MockOf`1

public T Generate()

Generate a single instance.

Returns

{T}

public T[] Generate(int count)

Generate count instances as an array.

Parameters

NameType
countSystem.Int32

Returns

{T}[]

public IEnumerable<T> GenerateLazy(int count)

Generate an IEnumerable<T> lazily, one instance at a time. With MockOf<T>.UseSeed(int), the seed stays active while the sequence is enumerated and the previous state returns when enumeration ends or is disposed.

Parameters

NameType
countSystem.Int32

Returns

System.Collections.Generic.IEnumerable{{T}}

public static implicit operator T(MockOf<T> builder)

Implicit single-item generation, e.g. Order o = builder;.

Parameters

NameType
builderMockMagic.MockOf`1

Returns

{T}