Skip to content

Commit

Permalink
More unit tests to further expand coverage around issue #965.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmg committed Mar 20, 2019
1 parent 8f92ac2 commit 3b6fd22
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/Autofac.Specification.Test/Features/DecoratorTests.cs
Expand Up @@ -281,6 +281,26 @@ public void DecoratorAppliedOnlyOnceToComponentWithExternalRegistrySource()
Assert.IsType<ImplementorA>(service.Decorated);
}

[Fact]
public void DecoratorCanBeAppliedTwiceIntentionallyWithExternalRegistrySource()
{
// #965: A nested lifetime scope that has a registration lambda
// causes the decorator to be applied twice - once for the container
// level, and once for the scope level.
var builder = new ContainerBuilder();
builder.RegisterType<ImplementorA>().As<IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var scope = container.BeginLifetimeScope(b => { });
var service = scope.Resolve<IDecoratedService>();

Assert.IsType<DecoratorA>(service);
Assert.IsType<DecoratorA>(service.Decorated);
Assert.IsType<ImplementorA>(service.Decorated.Decorated);
}

[Fact]
public void DecoratorCanBeAppliedTwice()
{
Expand Down
19 changes: 19 additions & 0 deletions test/Autofac.Test/Features/Decorators/OpenGenericDecoratorTests.cs
Expand Up @@ -550,6 +550,25 @@ public void DecoratorAppliedOnlyOnceToComponentWithExternalRegistrySource()
Assert.IsType<ImplementorA<int>>(service.Decorated);
}

[Fact]
public void DecoratorCanBeAppliedTwiceIntentionallyWithExternalRegistrySource()
{
// #965: A nested lifetime scope that has a registration lambda
// causes the decorator to be applied twice - once for the container
// level, and once for the scope level.
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(ImplementorA<>)).As(typeof(IDecoratedService<>));
builder.RegisterGenericDecorator(typeof(DecoratorA<>), typeof(IDecoratedService<>));
builder.RegisterGenericDecorator(typeof(DecoratorA<>), typeof(IDecoratedService<>));
var container = builder.Build();

var scope = container.BeginLifetimeScope(b => { });
var service = scope.Resolve<IDecoratedService<int>>();
Assert.IsType<DecoratorA<int>>(service);
Assert.IsType<DecoratorA<int>>(service.Decorated);
Assert.IsType<ImplementorA<int>>(service.Decorated.Decorated);
}

[Fact]
public void DecoratorCanBeAppliedTwice()
{
Expand Down

0 comments on commit 3b6fd22

Please sign in to comment.