Unit

Building Custom Class Composers

When your project needs specialized test fixture wiring—such as loading configuration sections, connecting to test containers, or resolving domain builders—you can implement a custom class composer in two small classes.

1. Implement ZeClassComposer<TAttribute> (or IZeClassComposer)

IZeClassComposer exposes a single method: object? Get(Type args). Inheriting ZeClassComposer<TAttribute> gives you access to all matching attributes declared on the test class via Attributes, while implementing IDisposable ensures ZeUnit disposes any owned resources when the class factory finishes.

using ZeUnit;
using ZeUnit.Composers;

public sealed class ClockComposer : ZeClassComposer<FakeClockAttribute>
{
    private readonly DateTimeOffset _fixedNow;

    public ClockComposer(ZeComposerAttribute[] attributes)
        : base(attributes)
    {
        var attr = Attributes.First();
        _fixedNow = DateTimeOffset.Parse(attr.IsoTimestamp);
    }

    public override object? Get(Type args)
    {
        if (args == typeof(TimeProvider))
            return new FixedTimeProvider(_fixedNow);

        return null;
    }
}

Return null when your composer does not handle args so ComposerClassFactory can fall through to the next attached composer.

Constructor Activation

ComposerClassFactory groups attributes by composer type and prefers passing ZeComposerAttribute[] (or falls back to a single ZeComposerAttribute or parameterless constructor). Accepting ZeComposerAttribute[] lets multiple stacked attributes of the same type cooperate in one composer instance.

2. Declare a ZeComposerAttribute<TComposer> Attribute

using ZeUnit;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class FakeClockAttribute(string isoTimestamp)
    : ZeComposerAttribute<ClockComposer>
{
    public string IsoTimestamp { get; } = isoTimestamp;
}

3. Apply It to a Test Suite

[FakeClock("2026-01-15T12:00:00Z")]
public class BillingScheduleTests(TimeProvider clock)
{
    public Fact UsesFixedTime() =>
        clock.GetUtcNow().Year.Is(2026);
}