Skip to content

Instantly share code, notes, and snippets.

@stefbast
Last active August 25, 2016 12:05
Show Gist options
  • Save stefbast/4af302a6987367c1e69b14cdb40ea66a to your computer and use it in GitHub Desktop.
Save stefbast/4af302a6987367c1e69b14cdb40ea66a to your computer and use it in GitHub Desktop.
using System.Linq;
using Autofac;
using Autofac.Builder;
using Autofac.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Tests
{
[TestClass]
public class When_container_builder_is_build
{
private IContainer _container;
private ContainerBuilder _containerBuilder;
protected override void Given()
{
_containerBuilder = containerBuilderFactory.GetContainerBuilder();
}
protected override void When()
{
_container = _containerBuilder.Build(ContainerBuildOptions.IgnoreStartableComponents);
}
[TestMethod]
public void Then_should_be_able_to_resolve_dependency_tree()
{
var servicesToCheck = _container.ComponentRegistry.Registrations.SelectMany(registration => registration.Services.OfType<IServiceWithType>());
foreach (var service in servicesToCheck)
{
CheckTypedService(service, _container);
CheckKeyedService(service, _container);
}
}
private static void CheckKeyedService(IServiceWithType service, IComponentContext context)
{
var keyedService = service as KeyedService;
if (keyedService != null)
{
context.ResolveKeyed(keyedService.ServiceKey, keyedService.ServiceType);
}
}
private static void CheckTypedService(IServiceWithType service, IComponentContext context)
{
var typedService = service as TypedService;
if (typedService != null)
{
context.Resolve(typedService.ServiceType);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment