Skip to content

Commit 02f5ce0

Browse files
committed
Add EntityFrameworkCore IMigrator mock (closes #264)
1 parent b67f3a0 commit 02f5ce0

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.EntityFrameworkCore
2+
{
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.EntityFrameworkCore.Migrations;
6+
using Microsoft.AspNetCore.Mvc.Internal;
7+
8+
public class MigratorMock : IMigrator
9+
{
10+
public string GenerateScript(
11+
string fromMigration = null,
12+
string toMigration = null,
13+
bool idempotent = false)
14+
=> string.Empty;
15+
16+
public void Migrate(string targetMigration = null)
17+
{
18+
// intentionally left empty
19+
}
20+
21+
public Task MigrateAsync(
22+
string targetMigration = null,
23+
CancellationToken cancellationToken = default(CancellationToken))
24+
=> TaskCache.CompletedTask;
25+
}
26+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
namespace MyTested.AspNetCore.Mvc.Internal.EntityFrameworkCore
22
{
33
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
4+
using Microsoft.EntityFrameworkCore.Migrations;
45
using Microsoft.EntityFrameworkCore.Storage.Internal;
56
using Microsoft.EntityFrameworkCore.ValueGeneration.Internal;
67
using Microsoft.Extensions.DependencyInjection;
78

89
public class ScopedInMemoryOptionsExtension : InMemoryOptionsExtension
910
{
1011
public override void ApplyServices(IServiceCollection services)
11-
{
12-
services
12+
=> services
13+
.AddScoped<IMigrator, MigratorMock>()
1314
.ReplaceLifetime<IInMemoryStoreSource>(ServiceLifetime.Scoped)
1415
.ReplaceLifetime<InMemoryValueGeneratorCache>(ServiceLifetime.Scoped);
15-
}
1616
}
1717
}

test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/ServicesTests.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace MyTested.AspNetCore.Mvc.Test
22
{
33
using System.Linq;
4-
using Internal;
4+
using Internal.EntityFrameworkCore;
55
using Microsoft.EntityFrameworkCore;
66
using Microsoft.EntityFrameworkCore.Internal;
77
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
@@ -16,8 +16,7 @@ public void ReplaceDbContextShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOn
1616
{
1717
var services = new ServiceCollection();
1818

19-
services.AddDbContext<CustomDbContext>(options =>
20-
options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"));
19+
this.AddDbContextWithSqlServer(services);
2120

2221
services.ReplaceDbContext();
2322

@@ -48,6 +47,26 @@ public void ReplaceDbContextShouldNotAddDbContextIfMissing()
4847
Assert.Null(serviceProvider.GetService<DbContext>());
4948
}
5049

50+
[Fact]
51+
public void CallingMigrateShouldNotThrowExceptionWithInMemoryDatabase()
52+
{
53+
var services = new ServiceCollection();
54+
55+
this.AddDbContextWithSqlServer(services);
56+
57+
services.ReplaceDbContext();
58+
59+
var serviceProvider = services.BuildServiceProvider();
60+
61+
services.BuildServiceProvider().GetRequiredService<CustomDbContext>().Database.Migrate();
62+
}
63+
64+
private void AddDbContextWithSqlServer(IServiceCollection services)
65+
{
66+
services.AddDbContext<CustomDbContext>(options =>
67+
options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"));
68+
}
69+
5170
private void AssertCorrectDbContextAndOptions(IServiceCollection services)
5271
{
5372
var serviceProvider = services.BuildServiceProvider();

0 commit comments

Comments
 (0)