Follow up to an older question here. Say I have a registration like the following:
container.Register(typeof(IHandleCommand<>), _handlerAssemblies, Lifestyle.Transient);
container.RegisterDecorator(typeof(IHandleCommand<>),
typeof(MetricsCommandHandlerWrapper<>), Lifestyle.Singleton);
Where the MetricsCommandHandlerWrapper
is defined like so:
public class MetricsCommandHandlerWrapper<T> : IHandleCommand<T> where T: ICommand
{
private readonly ICollectMetrics _metrics;
private readonly Func<IHandleCommand<T>> _handlerFactory;
public MetricsCommandHandlerWrapper(ICollectMetrics metrics,
Func<IHandleCommand<T>> handlerFactory)
{
_metrics = metrics;
_handlerFactory = handlerFactory;
}
public async Task HandleAsync(T command)
{
// code to record metrics around command handling which eventually invokes
await _handlerFactory().HandleAsync(command).ConfigureAwait(false);
}
}
How can I write a unit test that asserts the actual decoratee handlers are registered with Transient
lifestyle?
I have tried composing the root and inspecting the registration for a closed IHandleCommand<FakeCommand>
type, which reveals an ImplementationType
of MetricsCommandHandlerWrapper<FakeCommand>
as expected. Invoking GetRelationships()
on that registration reveals its 2 dependencies, ICollectMetrics
and the one I am interested in, the Func<IHandleCommand<FakeCommand>>
factory delegate, which is registered as a Singleton
. However invoking .Dependency.GetInstance()
on that factory delegate throws an exception that the instance producer returned null.
Is there any way to assert that the inner decoratee is registered as Transient
, and if so, how?