Skip to content

Commit

Permalink
Additional unit tests related to #965 to make sure decorators can be …
Browse files Browse the repository at this point in the history
…applied more than once if intentional.
  • Loading branch information
alexmg committed Mar 20, 2019
1 parent 7aa556a commit 8f92ac2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/Autofac.Specification.Test/Features/DecoratorTests.cs
Expand Up @@ -276,10 +276,47 @@ public void DecoratorAppliedOnlyOnceToComponentWithExternalRegistrySource()

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

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

[Fact]
public void DecoratorCanBeAppliedTwice()
{
var builder = new ContainerBuilder();
builder.RegisterType<ImplementorA>().As<IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var service = container.Resolve<IDecoratedService>();

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

[Fact]
public void DecoratorCanBeAppliedTwiceInChildLifetimeScope()
{
var builder = new ContainerBuilder();
builder.RegisterType<ImplementorA>().As<IDecoratedService>();
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var scope = container.BeginLifetimeScope(b => b.RegisterDecorator<DecoratorA, IDecoratedService>());
var scopeInstance = scope.Resolve<IDecoratedService>();

Assert.IsType<DecoratorA>(scopeInstance);
Assert.IsType<DecoratorA>(scopeInstance.Decorated);
Assert.IsType<ImplementorA>(scopeInstance.Decorated.Decorated);

var rootInstance = container.Resolve<IDecoratedService>();
Assert.IsType<DecoratorA>(rootInstance);
Assert.IsType<ImplementorA>(rootInstance.Decorated);
}

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

[Fact]
public void DecoratorCanBeAppliedTwice()
{
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 service = container.Resolve<IDecoratedService<int>>();

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

[Fact]
public void DecoratorCanBeAppliedTwiceInChildLifetimeScope()
{
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(ImplementorA<>)).As(typeof(IDecoratedService<>));
builder.RegisterGenericDecorator(typeof(DecoratorA<>), typeof(IDecoratedService<>));
var container = builder.Build();

var scope = container.BeginLifetimeScope(b => b.RegisterGenericDecorator(typeof(DecoratorA<>), typeof(IDecoratedService<>)));
var scopeInstance = scope.Resolve<IDecoratedService<int>>();

Assert.IsType<DecoratorA<int>>(scopeInstance);
Assert.IsType<DecoratorA<int>>(scopeInstance.Decorated);
Assert.IsType<ImplementorA<int>>(scopeInstance.Decorated.Decorated);

var rootInstance = container.Resolve<IDecoratedService<int>>();
Assert.IsType<DecoratorA<int>>(rootInstance);
Assert.IsType<ImplementorA<int>>(rootInstance.Decorated);
}

[Fact]
public void DecoratorCanBeAppliedToServiceRegisteredInChildLifetimeScope()
{
Expand Down

0 comments on commit 8f92ac2

Please sign in to comment.