Test showing scoped dependency bug still in 5.11

This commit is contained in:
James Doran 2019-12-07 06:05:24 +10:30
Родитель f213a66a66
Коммит 09f0f07151
3 изменённых файлов: 103 добавлений и 4 удалений

Просмотреть файл

@ -0,0 +1,74 @@
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Unity.Microsoft.DependencyInjection;
using Xunit;
namespace UnitTests
{
public class ScopedDepencencyTests
{
[Fact]
public void ScopedDependencyFromTransientFactoryNotSharedAcrossScopes()
{
// Arrange
var collection = new TestServiceCollection()
.AddTransient<ITransient>(CreateTransientFactory)
.AddScoped<IScoped, Scoped>();
var provider = collection.BuildServiceProvider();
// Act
ITransient transient1 = null;
ITransient transient2a = null;
ITransient transient2b = null;
using (var scope1 = provider.CreateScope())
{
transient1 = scope1.ServiceProvider.GetService<ITransient>();
}
using (var scope2 = provider.CreateScope())
{
transient2a = scope2.ServiceProvider.GetService<ITransient>();
transient2b = scope2.ServiceProvider.GetService<ITransient>();
}
// Assert
Assert.NotSame(transient1, transient2a);
Assert.NotSame(transient2a, transient2b);
Assert.NotSame(transient1.ScopedDependency, transient2a.ScopedDependency);
Assert.Same(transient2a.ScopedDependency, transient2b.ScopedDependency);
}
private ITransient CreateTransientFactory(System.IServiceProvider provider)
{
return provider.GetRequiredService<Transient>();
}
public interface ITransient
{
IScoped ScopedDependency { get; }
}
public class Transient : ITransient
{
public Transient(IScoped scoped)
{
ScopedDependency = scoped;
}
public IScoped ScopedDependency { get; }
}
public interface IScoped { }
public class Scoped : IScoped
{
}
}
internal class TestServiceCollection : List<ServiceDescriptor>, IServiceCollection
{
}
}

Просмотреть файл

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Unity.Container" Version="5.11.4" />
<PackageReference Include="Unity.Microsoft.DependencyInjection" Version="5.11.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
</Project>

Просмотреть файл

@ -1,13 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2005
# Visual Studio Version 16
VisualStudioVersion = 16.0.29418.71
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.DependencyInjection", "src\Unity.Microsoft.DependencyInjection.csproj", "{520251C7-14E6-434F-A26E-67E9762635BD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.Microsoft.DependencyInjection", "src\Unity.Microsoft.DependencyInjection.csproj", "{520251C7-14E6-434F-A26E-67E9762635BD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{DA61FEC3-1E17-4DCF-AC19-A6482A547741}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInjection.Tests", "tests\Unity.Microsoft.DependencyInjection.Tests.csproj", "{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.Microsoft.DependencyInjection.Tests", "tests\Unity.Microsoft.DependencyInjection.Tests.csproj", "{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -23,6 +25,10 @@ Global
{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}.Release|Any CPU.Build.0 = Release|Any CPU
{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE