Skip to content

Instantly share code, notes, and snippets.

@memark
Last active May 10, 2018 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memark/3718c5a3297b85e93b200bc9dbe4e63e to your computer and use it in GitHub Desktop.
Save memark/3718c5a3297b85e93b200bc9dbe4e63e to your computer and use it in GitHub Desktop.
private static void RegisterBusinessLogicStubs(ContainerBuilder containerBuilder)
{
var tempContainer = CreateBusinessLogicAutofacModuleContainer();
var interfaceToStubList = GetInterfaceTypeList(tempContainer);
var stubList = interfaceToStubList.Select(x => Substitute.For(new[] { x }, new object[] { }));
foreach (var s in stubList)
containerBuilder.RegisterInstance(s).AsImplementedInterfaces();
}
private static IContainer CreateBusinessLogicAutofacModuleContainer()
{
var containerBuilder = new ContainerBuilder();
var module = BusinessLogicAutofacModule.CreateWithoutDatabaseDefaults(default, default, default, default, default, default, default, default);
containerBuilder.RegisterModule(module);
return containerBuilder.Build();
}
private static IEnumerable<Type> GetInterfaceTypeList(IContainer container) =>
container.ComponentRegistry.Registrations
.SelectMany(x => x.Services)
.OfType<IServiceWithType>()
.Select(x => x.ServiceType)
.Where(x => x.IsInterface);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment