Unit

Lamar Dependency Injection

The ZeUnit.Lamar package integrates Lamar IoC containers directly into ZeUnit test class construction via [LamarContainer].

Installation

dotnet add package ZeUnit.Lamar

Defining a ServiceRegistry

Create a standard Lamar ServiceRegistry describing the services under test:

using Lamar;

public class CalculatorRegistry : ServiceRegistry
{
    public CalculatorRegistry()
    {
        For<ICalculator>().Use<StandardCalculator>();
        For<ITaxService>().Use<FlatRateTaxService>();
    }
}

Injecting Dependencies Into Test Classes

Decorate your test suite with [LamarContainer(typeof(CalculatorRegistry))] and request registered services in your primary constructor:

using ZeUnit;
using ZeUnit.Lamar;

[LamarContainer(typeof(CalculatorRegistry))]
public class CalculatorTests(ICalculator calculator, ITaxService taxService)
{
    public Fact ComputesTaxedTotal()
    {
        var subtotal = calculator.Add(100m, 25m);
        return taxService.Apply(subtotal).IsGreaterThan(125m);
    }
}

Graceful Fallback with Multiple Composers

LamarContainerClassComposer uses Lamar's container.TryGetInstance(args) when resolving each constructor parameter and disposes the underlying Container when the class factory is disposed. Because unresolved types return null rather than throwing, you can combine [LamarContainer] with [Fakes] from ZeUnit.FakeItEasy:

[LamarContainer(typeof(CalculatorRegistry))]
[Fakes]
public class PartialContainerTests(ICalculator realCalculator, Fake<IAuditLog> auditFake)
{
    public Fact MixesRealServicesAndFakes() =>
        realCalculator.IsNotNull();
}

Multiple Registries

[LamarContainer] supports AllowMultiple = true, so you can stack multiple ServiceRegistry types on a single test class and Lamar will merge all registrations into one container instance.