Skip to content

Commit

Permalink
nblumhardt#34: (failing) tests to show behavior with nested scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
srogovtsev committed Jun 25, 2020
1 parent 20dab2a commit cd93d6b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/AutofacSerilogIntegration.Tests/ActivatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks.Sources;
using Autofac;
using Autofac.Core;
using Autofac.Core.Activators.Reflection;
Expand Down Expand Up @@ -133,6 +134,38 @@ public void CustomStrategy_DelegateActivator_DependencyWithoutLogger_ShouldNotCr
VerifyLoggerCreation<DependencyWithoutLogger>(Times.Never);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void ReflectionActivator_DependencyWithoutLogger_WhenNestedScopes_ShouldNotCreateLogger(bool onlyKnownConsumers)
{
var containerBuilder = new ContainerBuilder();

containerBuilder.RegisterLogger(_logger.Object, onlyKnownConsumers: onlyKnownConsumers);

containerBuilder.RegisterType<DependencyWithoutLogger>();
containerBuilder.RegisterType<Component<DependencyWithoutLogger>>();

using (var container = containerBuilder.Build())
{
container.Resolve<Component<DependencyWithoutLogger>>();
//we know this to work from another test, just a sanity check
VerifyLoggerCreation<DependencyWithoutLogger>(Times.Never);

using (var scope1 = container.BeginLifetimeScope(builder => builder.RegisterType<Scoped1>()))
using (var scope2 = scope1.BeginLifetimeScope())
using (var scope3 = scope2.BeginLifetimeScope(builder => builder.RegisterType<Scoped2>()))
{
//this is to make things worse
for (var i = 0; i < 5; i++) scope3.Resolve<Component<DependencyWithoutLogger>>();

VerifyLoggerCreation<DependencyWithoutLogger>(Times.Never);
}
}


}

private class Strategy : IRegistrationProcessor
{
public Type Process(IComponentRegistration registration, out bool injectParameter, out PropertyInfo[] targetProperties)
Expand Down Expand Up @@ -166,5 +199,8 @@ public DependencyWithLogger(ILogger logger)
}

private class DependencyWithoutLogger {}

private class Scoped1 { }
private class Scoped2 { }
}
}

0 comments on commit cd93d6b

Please sign in to comment.