Created
March 19, 2014 19:06
-
-
Save drusellers/9648955 to your computer and use it in GitHub Desktop.
Failing AutoFac Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Autofac.Core; | |
using NUnit.Framework; | |
namespace Autofac.Tests | |
{ | |
public class ExampleLogger | |
{ | |
class RootSetup | |
{ | |
public RootSetup(DemoLogger demoLogger) | |
{ | |
} | |
} | |
class DemoLogger | |
{ | |
readonly string _className; | |
public DemoLogger(string className) | |
{ | |
_className = className; | |
} | |
} | |
class LifetimeSetup | |
{ | |
public LifetimeSetup(DemoLogger demoLogger) | |
{ | |
} | |
} | |
[SetUp] | |
public void SetUp() | |
{ | |
onPreparationCount = 0; | |
} | |
static int onPreparationCount; | |
class FancyModule : Module | |
{ | |
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) | |
{ | |
registration.Preparing += OnComponentPreparing; | |
} | |
void OnComponentPreparing(object sender, PreparingEventArgs e) | |
{ | |
onPreparationCount++; | |
var t = e.Component.Activator.LimitType; | |
e.Parameters = e.Parameters.Union(new[] | |
{ | |
new ResolvedParameter((p, i) => p.ParameterType == typeof(DemoLogger), (p, i) => new DemoLogger(t.FullName)), | |
}); | |
} | |
} | |
IContainer BuildContainer() | |
{ | |
var builder = new ContainerBuilder(); | |
builder.RegisterModule<FancyModule>(); | |
builder.RegisterType<RootSetup>().AsSelf(); | |
var container = builder.Build(); | |
return container; | |
} | |
[Test] | |
public void WithTag() | |
{ | |
var container = BuildContainer(); | |
container.Resolve<RootSetup>(); | |
Assert.That(onPreparationCount, Is.EqualTo(1)); | |
using (var scope = container.BeginLifetimeScope("abc")) | |
{ | |
scope.Resolve<RootSetup>(); | |
Assert.That(onPreparationCount, Is.EqualTo(2)); | |
} | |
} | |
[Test] | |
public void WithoutTag() | |
{ | |
var container = BuildContainer(); | |
container.Resolve<RootSetup>(); | |
Assert.That(onPreparationCount, Is.EqualTo(1)); | |
using (var scope = container.BeginLifetimeScope()) | |
{ | |
scope.Resolve<RootSetup>(); | |
Assert.That(onPreparationCount, Is.EqualTo(2)); | |
} | |
} | |
[Test] | |
public void WithInnerAdds() | |
{ | |
var container = BuildContainer(); | |
container.Resolve<RootSetup>(); | |
Assert.That(onPreparationCount, Is.EqualTo(1)); | |
using (var scope = container.BeginLifetimeScope(cfg => | |
{ | |
cfg.RegisterType<LifetimeSetup>().AsSelf(); | |
})) | |
{ | |
scope.Resolve<LifetimeSetup>(); | |
Assert.That(onPreparationCount, Is.EqualTo(2)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment