Case Study: FakeItEasy Composers
ZeUnit.FakeItEasy eliminates repetitive mock boilerplate by automatically creating and configuring Fake<T> dependencies directly through class constructor injection.
Installation
dotnet add package ZeUnit.FakeItEasy
Automatic On-Demand Fakes with [Fakes]
Add [Fakes] to any test class and request Fake<TService> parameters in the constructor. FakesClassComposer instantiates each requested Fake<T> dynamically:
using FakeItEasy;
using ZeUnit;
using ZeUnit.FakeItEasy;
[Fakes]
public class PaymentProcessorTests(Fake<IPaymentGateway> gatewayFake)
{
public Fact ChargesExpectedAmount()
{
var gateway = gatewayFake.FakedObject;
A.CallTo(() => gateway.Charge(49.99m)).Returns(true);
var processor = new PaymentProcessor(gateway);
var charged = processor.ProcessOrder(49.99m);
A.CallTo(() => gateway.Charge(49.99m)).MustHaveHappenedOnceExactly();
return charged.True();
}
}
Pre-Configured Domain Fakes with FakeAttribute<TAttribute, TType>
When many suites need the same mock setup (such as an authenticated user context or stubbed repository), subclass FakeAttribute<TAttribute, TType> and override Build(Fake<TType>):
using FakeItEasy;
using ZeUnit.FakeItEasy;
public sealed class StubClockFakeAttribute
: FakeAttribute<StubClockFakeAttribute, ISystemClock>
{
protected override Fake<ISystemClock> Build(Fake<ISystemClock> fake)
{
A.CallTo(() => fake.FakedObject.UtcNow)
.Returns(new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc));
return fake;
}
}
[StubClockFake]
public class InvoiceDateTests(Fake<ISystemClock> clockFake)
{
public Fact UsesStubbedYear() =>
clockFake.FakedObject.UtcNow.Year.Is(2026);
}
Scoped Composer Order
Combine [AutofacModule<T>] or [LamarContainer(typeof(T))] with [Fakes] to supply real implementations from your DI container while automatically falling back to Fake<T> for un-registered collaborators.