From 45b6cef64db141791091d89ef36ec0c9ef38560c Mon Sep 17 00:00:00 2001 From: Dan H Date: Fri, 11 Mar 2022 11:05:05 +0000 Subject: [PATCH] Adding failing unit test for issue where disposable objects are not disposed by DI when decorated. --- test/Scrutor.Tests/DecorationTests.cs | 86 ++++++++++++--------------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/test/Scrutor.Tests/DecorationTests.cs b/test/Scrutor.Tests/DecorationTests.cs index afc1281..8e2851e 100644 --- a/test/Scrutor.Tests/DecorationTests.cs +++ b/test/Scrutor.Tests/DecorationTests.cs @@ -38,10 +38,9 @@ public void CanDecorateMultipleLevels() var instance = provider.GetRequiredService(); - var decorator = Assert.IsType(instance); - var outerDecorator = Assert.IsType(decorator.Inner); - - Assert.IsType(outerDecorator.Inner); + var outerDecorator = Assert.IsType(instance); + var innerDecorator = Assert.IsType(outerDecorator.Inner); + _ = Assert.IsType(innerDecorator.Inner); } [Fact] @@ -228,10 +227,10 @@ public void Issue148_Decorate_IsAbleToDecorateConcreateTypes() Assert.NotNull(result); var inner = Assert.IsType(result.Inner); Assert.NotNull(inner.Dependency); - } - - #region Individual functions tests - + } + + #region Individual functions tests + [Fact] public void DecorationFunctionsDoDecorateRegisteredService() { @@ -242,9 +241,9 @@ public void DecorationFunctionsDoDecorateRegisteredService() sc => sc.Decorate(typeof(IDecoratedService), typeof(Decorator)), sc => sc.TryDecorate(typeof(IDecoratedService), typeof(Decorator)), sc => sc.Decorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)), - sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)), + sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)), sc => sc.Decorate((IDecoratedService obj) => new Decorator(obj)), - sc => sc.TryDecorate((IDecoratedService obj) => new Decorator(obj)), + sc => sc.TryDecorate((IDecoratedService obj) => new Decorator(obj)), sc => sc.Decorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) => new Decorator((IDecoratedService)obj)), sc => sc.TryDecorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) => new Decorator((IDecoratedService)obj)), sc => sc.Decorate(typeof(IDecoratedService), (object obj) => new Decorator((IDecoratedService)obj)), @@ -263,8 +262,8 @@ public void DecorationFunctionsDoDecorateRegisteredService() var decorator = Assert.IsType(instance); Assert.IsType(decorator.Inner); } - } - + } + [Fact] public void DecorationFunctionsProvideScopedServiceProvider() { @@ -272,24 +271,24 @@ public void DecorationFunctionsProvideScopedServiceProvider() var decorationFunctions = new Action[] { - sc => sc.Decorate((IDecoratedService obj, IServiceProvider sp) => + sc => sc.Decorate((IDecoratedService obj, IServiceProvider sp) => { actual = sp; - return null; + return null; }), sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => { actual = sp; - return null; - }), + return null; + }), sc => sc.Decorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) => { - actual = sp; - return null; + actual = sp; + return null; }), sc => sc.TryDecorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) => { - actual = sp; + actual = sp; return null; }), }; @@ -300,12 +299,12 @@ public void DecorationFunctionsProvideScopedServiceProvider() { services.AddScoped(); decorationMethod(services); - }); - - using var scope = provider.CreateScope(); - var expected = scope.ServiceProvider; - _ = scope.ServiceProvider.GetService(); - Assert.Same(expected, actual); + }); + + using var scope = provider.CreateScope(); + var expected = scope.ServiceProvider; + _ = scope.ServiceProvider.GetService(); + Assert.Same(expected, actual); } } @@ -327,8 +326,8 @@ public void TryDecorateReturnsBoolResult() { sc => sc.TryDecorate(), sc => sc.TryDecorate(typeof(IDecoratedService), typeof(Decorator)), - sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)), - sc => sc.TryDecorate((IDecoratedService obj) => new Decorator(obj)), + sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)), + sc => sc.TryDecorate((IDecoratedService obj) => new Decorator(obj)), sc => sc.TryDecorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) => new Decorator((IDecoratedService)obj)), sc => sc.TryDecorate(typeof(IDecoratedService), (object obj) => new Decorator((IDecoratedService)obj)) }; @@ -344,14 +343,14 @@ public void TryDecorateReturnsBoolResult() isDecorated = decorationMethod(services); Assert.True(isDecorated); - }); + }); } - } - - #endregion - + } + + #endregion + #region DI Scope test - + [Fact] public void DecoratedTransientServiceRetainsScope() { @@ -435,12 +434,12 @@ public void DependentServicesRetainTheirOwnScope() Assert.NotEqual(decorator1, decorator2); Assert.NotEqual(decorator1.Inner, decorator2.Inner); Assert.Equal(decorator1.Inner.Dependency, decorator2.Inner.Dependency); - } - + } + #endregion - + #region Mocks - + public interface IDecoratedService { } public class DecoratedService @@ -482,12 +481,7 @@ public class Decorator : IDecoratedService { public Decorator(IDecoratedService inner, IService injectedService = null) { - if (inner == null) - { - throw new ArgumentNullException(nameof(inner)); - } - - Inner = inner; + Inner = inner ?? throw new ArgumentNullException(nameof(inner)); InjectedService = injectedService; } @@ -524,11 +518,7 @@ public DisposableServiceDecorator(IDisposableService inner) public bool WasDisposed { get; private set; } - public void Dispose() - { - Inner.Dispose(); - WasDisposed = true; - } + public void Dispose() => WasDisposed = true; } public interface IEvent