Skip to content

Commit 3b6fd22

Browse files
committed
More unit tests to further expand coverage around issue #965.
1 parent 8f92ac2 commit 3b6fd22

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

test/Autofac.Specification.Test/Features/DecoratorTests.cs

+20
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,26 @@ public void DecoratorAppliedOnlyOnceToComponentWithExternalRegistrySource()
281281
Assert.IsType<ImplementorA>(service.Decorated);
282282
}
283283

284+
[Fact]
285+
public void DecoratorCanBeAppliedTwiceIntentionallyWithExternalRegistrySource()
286+
{
287+
// #965: A nested lifetime scope that has a registration lambda
288+
// causes the decorator to be applied twice - once for the container
289+
// level, and once for the scope level.
290+
var builder = new ContainerBuilder();
291+
builder.RegisterType<ImplementorA>().As<IDecoratedService>();
292+
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
293+
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
294+
var container = builder.Build();
295+
296+
var scope = container.BeginLifetimeScope(b => { });
297+
var service = scope.Resolve<IDecoratedService>();
298+
299+
Assert.IsType<DecoratorA>(service);
300+
Assert.IsType<DecoratorA>(service.Decorated);
301+
Assert.IsType<ImplementorA>(service.Decorated.Decorated);
302+
}
303+
284304
[Fact]
285305
public void DecoratorCanBeAppliedTwice()
286306
{

test/Autofac.Test/Features/Decorators/OpenGenericDecoratorTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,25 @@ public void DecoratorAppliedOnlyOnceToComponentWithExternalRegistrySource()
550550
Assert.IsType<ImplementorA<int>>(service.Decorated);
551551
}
552552

553+
[Fact]
554+
public void DecoratorCanBeAppliedTwiceIntentionallyWithExternalRegistrySource()
555+
{
556+
// #965: A nested lifetime scope that has a registration lambda
557+
// causes the decorator to be applied twice - once for the container
558+
// level, and once for the scope level.
559+
var builder = new ContainerBuilder();
560+
builder.RegisterGeneric(typeof(ImplementorA<>)).As(typeof(IDecoratedService<>));
561+
builder.RegisterGenericDecorator(typeof(DecoratorA<>), typeof(IDecoratedService<>));
562+
builder.RegisterGenericDecorator(typeof(DecoratorA<>), typeof(IDecoratedService<>));
563+
var container = builder.Build();
564+
565+
var scope = container.BeginLifetimeScope(b => { });
566+
var service = scope.Resolve<IDecoratedService<int>>();
567+
Assert.IsType<DecoratorA<int>>(service);
568+
Assert.IsType<DecoratorA<int>>(service.Decorated);
569+
Assert.IsType<ImplementorA<int>>(service.Decorated.Decorated);
570+
}
571+
553572
[Fact]
554573
public void DecoratorCanBeAppliedTwice()
555574
{

0 commit comments

Comments
 (0)