Skip to content

Commit 168b72f

Browse files
committed
Added unit test for replacing Entity Framework database (#125)
1 parent 48e4d37 commit 168b72f

File tree

8 files changed

+460
-3
lines changed

8 files changed

+460
-3
lines changed

MyTested.AspNetCore.Mvc.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Dat
6262
EndProject
6363
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.EntityFrameworkCore", "src\MyTested.AspNetCore.Mvc.EntityFrameworkCore\MyTested.AspNetCore.Mvc.EntityFrameworkCore.xproj", "{0BE034FD-B68B-4D52-A0C5-29BCDBF543DC}"
6464
EndProject
65+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test", "test\MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test\MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test.xproj", "{285E08F2-2E5A-4226-B867-FD3DF7C52053}"
66+
EndProject
6567
Global
6668
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6769
Debug|Any CPU = Debug|Any CPU
@@ -156,6 +158,10 @@ Global
156158
{0BE034FD-B68B-4D52-A0C5-29BCDBF543DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
157159
{0BE034FD-B68B-4D52-A0C5-29BCDBF543DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
158160
{0BE034FD-B68B-4D52-A0C5-29BCDBF543DC}.Release|Any CPU.Build.0 = Release|Any CPU
161+
{285E08F2-2E5A-4226-B867-FD3DF7C52053}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
162+
{285E08F2-2E5A-4226-B867-FD3DF7C52053}.Debug|Any CPU.Build.0 = Debug|Any CPU
163+
{285E08F2-2E5A-4226-B867-FD3DF7C52053}.Release|Any CPU.ActiveCfg = Release|Any CPU
164+
{285E08F2-2E5A-4226-B867-FD3DF7C52053}.Release|Any CPU.Build.0 = Release|Any CPU
159165
EndGlobalSection
160166
GlobalSection(SolutionProperties) = preSolution
161167
HideSolutionNode = FALSE
@@ -185,5 +191,6 @@ Global
185191
{2C375C22-4710-438F-A4C2-325C7D091710} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
186192
{BAAAAB60-49AE-4BFE-B176-408C86746474} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
187193
{0BE034FD-B68B-4D52-A0C5-29BCDBF543DC} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
194+
{285E08F2-2E5A-4226-B867-FD3DF7C52053} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
188195
EndGlobalSection
189196
EndGlobal

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/ServiceCollectionEntityFrameworkCoreExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public static void ReplaceDbContext(this IServiceCollection serviceCollection)
4040

4141
if (existingDbContext != null)
4242
{
43+
if (existingDbContext.Lifetime != ServiceLifetime.Scoped)
44+
{
45+
serviceCollection.Replace(
46+
existingDbContext.ServiceType,
47+
existingDbContext.ImplementationFactory,
48+
ServiceLifetime.Scoped);
49+
}
50+
4351
var genericMethod = ReplaceDatabaseMethodInfo.MakeGenericMethod(existingDbContext.ImplementationType);
4452
genericMethod.Invoke(null, new object[] { serviceCollection });
4553
}

test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test.xproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
55
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
66
</PropertyGroup>
7-
87
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
98
<PropertyGroup Label="Globals">
109
<ProjectGuid>285e08f2-2e5a-4226-b867-fd3df7c52053</ProjectGuid>
@@ -13,9 +12,11 @@
1312
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
1413
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
1514
</PropertyGroup>
16-
1715
<PropertyGroup>
1816
<SchemaVersion>2.0</SchemaVersion>
1917
</PropertyGroup>
18+
<ItemGroup>
19+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
20+
</ItemGroup>
2021
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21-
</Project>
22+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,49 @@
11
namespace MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test
22
{
3+
using System.Linq;
4+
using Internal;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.EntityFrameworkCore.Internal;
7+
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Setups.Common;
10+
using Xunit;
11+
312
public class ServicesTests
413
{
14+
[Fact]
15+
public static void ReplaceDbContextShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne()
16+
{
17+
var services = new ServiceCollection();
18+
19+
services.AddDbContext<CustomDbContext>(options =>
20+
options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"));
21+
22+
services.ReplaceDbContext();
23+
24+
var serviceProvider = services.BuildServiceProvider();
25+
26+
var dbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(CustomDbContext));
27+
28+
Assert.NotNull(dbContextService);
29+
Assert.Equal(ServiceLifetime.Scoped, dbContextService.Lifetime);
30+
31+
var customDbContext = serviceProvider.GetService<CustomDbContext>();
32+
33+
Assert.NotNull(customDbContext);
34+
35+
var dbContextOptions = serviceProvider.GetService<DbContextOptions<CustomDbContext>>();
36+
37+
Assert.NotNull(dbContextOptions);
38+
Assert.Equal(3, dbContextOptions.Extensions.Count());
39+
40+
var coreOptionsExtension = dbContextOptions.FindExtension<CoreOptionsExtension>();
41+
var inMemoryOptionsExtension = dbContextOptions.FindExtension<InMemoryOptionsExtension>();
42+
var scopedInMemoryOptionsExtension = dbContextOptions.FindExtension<ScopedInMemoryOptionsExtension>();
43+
44+
Assert.NotNull(coreOptionsExtension);
45+
Assert.NotNull(inMemoryOptionsExtension);
46+
Assert.NotNull(scopedInMemoryOptionsExtension);
47+
}
548
}
649
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test.Setups.Common
2+
{
3+
using Microsoft.EntityFrameworkCore;
4+
5+
public class CustomDbContext : DbContext
6+
{
7+
public CustomDbContext(DbContextOptions<CustomDbContext> options)
8+
: base(options)
9+
{
10+
}
11+
12+
public DbSet<CustomModel> Models { get; set; }
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test.Setups.Common
2+
{
3+
public class CustomModel
4+
{
5+
public int Id { get; set; }
6+
7+
public string Name { get; set; }
8+
}
9+
}

test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dotnet-test-xunit": "1.0.0-*",
1616
"xunit": "2.1.0",
1717
"Microsoft.AspNetCore.Mvc": "1.0.0-*",
18+
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-*",
1819
"MyTested.AspNetCore.Mvc.EntityFrameworkCore": "1.0.0-*",
1920
"MyTested.AspNetCore.Mvc.Test": "1.0.0-*"
2021
},

0 commit comments

Comments
 (0)