Skip to content

Commit a569575

Browse files
committed
DbContext can now be registered and used through an interface during tests (closes #379)
1 parent bf42979 commit a569575

File tree

25 files changed

+759
-135
lines changed

25 files changed

+759
-135
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Services/TestServiceProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public static TInstance TryGetService<TInstance>()
8787
public static void SaveServiceLifetime(Type serviceType, ServiceLifetime lifetime)
8888
=> ServiceLifetimes[serviceType] = lifetime;
8989

90+
public static void SaveServiceLifetime<TService>(ServiceLifetime lifetime)
91+
=> SaveServiceLifetime(typeof(TService), lifetime);
92+
9093
public static ServiceLifetime GetServiceLifetime(Type serviceType)
9194
=> ServiceLifetimes.ContainsKey(serviceType)
9295
? ServiceLifetimes[serviceType]

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface IWithDbContextBuilder
2323
/// <param name="entities">Initial values to add to the provided <see cref="DbContext"/>.</param>
2424
/// <returns>The same <see cref="DbContext"/> builder.</returns>
2525
IAndWithDbContextBuilder WithEntities<TDbContext>(IEnumerable<object> entities)
26-
where TDbContext : DbContext;
26+
where TDbContext : class;
2727

2828
/// <summary>
2929
/// Sets initial values to the registered <see cref="DbContext"/>.
@@ -39,7 +39,7 @@ IAndWithDbContextBuilder WithEntities<TDbContext>(IEnumerable<object> entities)
3939
/// <param name="entities">Initial values to add to the provided <see cref="DbContext"/>.</param>
4040
/// <returns>The same <see cref="DbContext"/> builder.</returns>
4141
IAndWithDbContextBuilder WithEntities<TDbContext>(params object[] entities)
42-
where TDbContext : DbContext;
42+
where TDbContext : class;
4343

4444
/// <summary>
4545
/// Sets initial values to the registered <see cref="DbContext"/>.
@@ -55,7 +55,7 @@ IAndWithDbContextBuilder WithEntities<TDbContext>(params object[] entities)
5555
/// <param name="dbContextSetup">Action setting the <see cref="DbContext"/>.</param>
5656
/// <returns>The same <see cref="DbContext"/> builder.</returns>
5757
IAndWithDbContextBuilder WithEntities<TDbContext>(Action<TDbContext> dbContextSetup)
58-
where TDbContext : DbContext;
58+
where TDbContext : class;
5959

6060
/// <summary>
6161
/// Sets initial values to the provided <see cref="DbContext"/> entity.
@@ -74,7 +74,7 @@ IAndWithDbContextBuilder WithSet<TEntity>(Action<DbSet<TEntity>> entitySetup)
7474
/// <param name="entitySetup">Action setting the <see cref="DbContext"/> entity.</param>
7575
/// <returns>The same <see cref="DbContext"/> builder.</returns>
7676
IAndWithDbContextBuilder WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>> entitySetup)
77-
where TDbContext : DbContext
77+
where TDbContext : class
7878
where TEntity : class;
7979
}
8080
}

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextTestBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public interface IWithDbContextTestBuilder
2828
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/>.</typeparam>
2929
/// <param name="assertions">Action containing all assertions for the <see cref="DbContext"/> entities.</param>
3030
IAndWithDbContextTestBuilder WithEntities<TDbContext>(Action<TDbContext> assertions)
31-
where TDbContext : DbContext;
31+
where TDbContext : class;
3232

3333
/// <summary>
3434
/// Tests whether <see cref="DbContext"/> entities pass the given predicate.
3535
/// </summary>
3636
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/>.</typeparam>
3737
/// <param name="predicate">Predicate testing the <see cref="DbContext"/> entities.</param>
3838
IAndWithDbContextTestBuilder WithEntities<TDbContext>(Func<TDbContext, bool> predicate)
39-
where TDbContext : DbContext;
39+
where TDbContext : class;
4040

4141
/// <summary>
4242
/// Tests whether <see cref="DbContext"/> entity <see cref="DbSet{TEntity}"/> passes the given assertions.
@@ -61,7 +61,7 @@ IAndWithDbContextTestBuilder WithSet<TEntity>(Func<DbSet<TEntity>, bool> predica
6161
/// <typeparam name="TEntity">Type of entity set.</typeparam>
6262
/// <param name="assertions">Action containing all assertions for the <see cref="DbContext"/> entity set.</param>
6363
IAndWithDbContextTestBuilder WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>> assertions)
64-
where TDbContext : DbContext
64+
where TDbContext : class
6565
where TEntity : class;
6666

6767
/// <summary>
@@ -71,7 +71,7 @@ IAndWithDbContextTestBuilder WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>>
7171
/// <typeparam name="TEntity">Type of entity set.</typeparam>
7272
/// <param name="predicate">Predicate testing the <see cref="DbContext"/> entity set.</param>
7373
IAndWithDbContextTestBuilder WithSet<TDbContext, TEntity>(Func<DbSet<TEntity>, bool> predicate)
74-
where TDbContext : DbContext
74+
where TDbContext : class
7575
where TEntity : class;
7676
}
7777
}

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithoutDbContextBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ IAndWithoutDbContextBuilder WithoutEntityByKey<TEntity>(object key)
2323
/// <param name="key">Primary key of entity to remove from the registered <see cref="DbContext"/>.</param>
2424
/// <returns>The same <see cref="DbContext"/> builder.</returns>
2525
IAndWithoutDbContextBuilder WithoutEntityByKey<TDbContext, TEntity>(object key)
26-
where TDbContext : DbContext;
26+
where TDbContext : class;
2727

2828
/// <summary>
2929
/// Remove entities by providing their primary keys from the registered <see cref="DbContext"/>.
@@ -42,7 +42,7 @@ IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TEntity>(IEnumerable<object> k
4242
/// <param name="keys">Primary keys for entities to remove from the registered <see cref="DbContext"/>.</param>
4343
/// <returns>The same <see cref="DbContext"/> builder.</returns>
4444
IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TDbContext, TEntity>(IEnumerable<object> keys)
45-
where TDbContext : DbContext;
45+
where TDbContext : class;
4646

4747
/// <summary>
4848
/// Remove entity from the registered <see cref="DbContext"/>.
@@ -58,7 +58,7 @@ IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TDbContext, TEntity>(IEnumerab
5858
/// <param name="entity">Entity to remove from the registered <see cref="DbContext"/>.</param>
5959
/// <returns>The same <see cref="DbContext"/> builder.</returns>
6060
IAndWithoutDbContextBuilder WithoutEntity<TDbContext>(object entity)
61-
where TDbContext : DbContext;
61+
where TDbContext : class;
6262

6363
/// <summary>
6464
/// Remove values from the registered <see cref="DbContext"/>.
@@ -74,7 +74,7 @@ IAndWithoutDbContextBuilder WithoutEntity<TDbContext>(object entity)
7474
/// <param name="entities">Values to remove from the registered <see cref="DbContext"/>.</param>
7575
/// <returns>The same <see cref="DbContext"/> builder.</returns>
7676
IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(IEnumerable<object> entities)
77-
where TDbContext : DbContext;
77+
where TDbContext : class;
7878

7979
/// <summary>
8080
/// Remove values from the registered <see cref="DbContext"/>.
@@ -90,7 +90,7 @@ IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(IEnumerable<object> enti
9090
/// <param name="entities">Values to remove from the provided <see cref="DbContext"/>.</param>
9191
/// <returns>The same <see cref="DbContext"/> builder.</returns>
9292
IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(params object[] entities)
93-
where TDbContext : DbContext;
93+
where TDbContext : class;
9494

9595
/// <summary>
9696
/// Remove values from the registered <see cref="DbContext"/>.
@@ -106,7 +106,7 @@ IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(params object[] entities
106106
/// <param name="dbContextSetup">Action setting the <see cref="DbContext"/>.</param>
107107
/// <returns>The same <see cref="DbContext"/> builder.</returns>
108108
IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(Action<TDbContext> dbContextSetup)
109-
where TDbContext : DbContext;
109+
where TDbContext : class;
110110

111111
/// <summary>
112112
/// Remove values from the provided <see cref="DbContext"/> entity.
@@ -125,7 +125,7 @@ IAndWithoutDbContextBuilder WithoutSet<TEntity>(Action<DbSet<TEntity>> entitySet
125125
/// <param name="entitySetup">Action setting the <see cref="DbContext"/> entity.</param>
126126
/// <returns>The same <see cref="DbContext"/> builder.</returns>
127127
IAndWithoutDbContextBuilder WithoutSet<TDbContext, TEntity>(Action<DbSet<TEntity>> entitySetup)
128-
where TDbContext : DbContext
128+
where TDbContext : class
129129
where TEntity : class;
130130

131131
/// <summary>

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public IAndWithDbContextBuilder WithEntities(IEnumerable<object> entities)
2929

3030
/// <inheritdoc />
3131
public IAndWithDbContextBuilder WithEntities<TDbContext>(IEnumerable<object> entities)
32-
where TDbContext : DbContext
33-
=> this.WithEntities<TDbContext>(dbContext => dbContext.AddRange(entities));
32+
where TDbContext : class
33+
=> this.WithEntities<TDbContext>(dbContext => (dbContext as DbContext).AddRange(entities));
3434

3535
/// <inheritdoc />
3636
public IAndWithDbContextBuilder WithEntities(params object[] entities)
3737
=> this.WithEntities(entities.AsEnumerable());
3838

3939
/// <inheritdoc />
4040
public IAndWithDbContextBuilder WithEntities<TDbContext>(params object[] entities)
41-
where TDbContext : DbContext
41+
where TDbContext : class
4242
=> this.WithEntities<TDbContext>(entities.AsEnumerable());
4343

4444
/// <inheritdoc />
@@ -47,13 +47,13 @@ public IAndWithDbContextBuilder WithEntities(Action<DbContext> dbContextSetup)
4747

4848
/// <inheritdoc />
4949
public IAndWithDbContextBuilder WithEntities<TDbContext>(Action<TDbContext> dbContextSetup)
50-
where TDbContext : DbContext
50+
where TDbContext : class
5151
{
5252
CommonValidator.CheckForNullReference(dbContextSetup, nameof(dbContextSetup));
5353

5454
var dbContext = this.TestContext.GetDbContext<TDbContext>();
5555
dbContextSetup(dbContext);
56-
dbContext.SaveChanges();
56+
(dbContext as DbContext).SaveChanges();
5757

5858
return this;
5959
}
@@ -65,12 +65,12 @@ public IAndWithDbContextBuilder WithSet<TEntity>(Action<DbSet<TEntity>> entitySe
6565

6666
/// <inheritdoc />
6767
public IAndWithDbContextBuilder WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>> entitySetup)
68-
where TDbContext : DbContext
68+
where TDbContext : class
6969
where TEntity : class
7070
{
7171
CommonValidator.CheckForNullReference(entitySetup, nameof(entitySetup));
7272

73-
var dbContext = this.TestContext.GetDbContext<TDbContext>();
73+
var dbContext = this.TestContext.GetBaseDbContext<TDbContext>();
7474
entitySetup(dbContext.Set<TEntity>());
7575
dbContext.SaveChanges();
7676

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextTestBuilder.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,15 @@ public WithDbContextTestBuilder(ComponentTestContext testContext)
2424
}
2525

2626
/// <inheritdoc />
27-
public IAndWithDbContextTestBuilder WithEntities(Action<DbContext> assertions)
28-
{
29-
return this.WithEntities<DbContext>(assertions);
30-
}
27+
public IAndWithDbContextTestBuilder WithEntities(Action<DbContext> assertions)
28+
=> this.WithEntities<DbContext>(assertions);
3129

3230
/// <inheritdoc />
33-
public IAndWithDbContextTestBuilder WithEntities(Func<DbContext, bool> predicate)
34-
{
35-
return this.WithEntities<DbContext>(predicate);
36-
}
31+
public IAndWithDbContextTestBuilder WithEntities(Func<DbContext, bool> predicate)
32+
=> this.WithEntities<DbContext>(predicate);
3733

3834
/// <inheritdoc />
39-
public IAndWithDbContextTestBuilder WithEntities<TDbContext>(Func<TDbContext, bool> predicate) where TDbContext : DbContext
35+
public IAndWithDbContextTestBuilder WithEntities<TDbContext>(Func<TDbContext, bool> predicate) where TDbContext : class
4036
{
4137
CommonValidator.CheckForNullReference(predicate, nameof(predicate));
4238

@@ -52,7 +48,7 @@ public IAndWithDbContextTestBuilder WithEntities<TDbContext>(Func<TDbContext, bo
5248
}
5349

5450
/// <inheritdoc />
55-
public IAndWithDbContextTestBuilder WithEntities<TDbContext>(Action<TDbContext> assertions) where TDbContext : DbContext
51+
public IAndWithDbContextTestBuilder WithEntities<TDbContext>(Action<TDbContext> assertions) where TDbContext : class
5652
{
5753
CommonValidator.CheckForNullReference(assertions, nameof(assertions));
5854

@@ -63,38 +59,34 @@ public IAndWithDbContextTestBuilder WithEntities<TDbContext>(Action<TDbContext>
6359

6460
/// <inheritdoc />
6561
public IAndWithDbContextTestBuilder WithSet<TEntity>(Action<DbSet<TEntity>> assertions)
66-
where TEntity : class
67-
{
68-
return this.WithSet<DbContext, TEntity>(assertions);
69-
}
62+
where TEntity : class
63+
=> this.WithSet<DbContext, TEntity>(assertions);
7064

7165
/// <inheritdoc />
7266
public IAndWithDbContextTestBuilder WithSet<TEntity>(Func<DbSet<TEntity>, bool> predicate)
73-
where TEntity : class
74-
{
75-
return this.WithSet<DbContext, TEntity>(predicate);
76-
}
67+
where TEntity : class
68+
=> this.WithSet<DbContext, TEntity>(predicate);
7769

7870
/// <inheritdoc />
7971
public IAndWithDbContextTestBuilder WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>> assertions)
80-
where TDbContext : DbContext
72+
where TDbContext : class
8173
where TEntity : class
8274
{
8375
CommonValidator.CheckForNullReference(assertions, nameof(assertions));
8476

85-
assertions(this.TestContext.GetDbContext<TDbContext>().Set<TEntity>());
77+
assertions(this.TestContext.GetBaseDbContext<TDbContext>().Set<TEntity>());
8678

8779
return this;
8880
}
8981

9082
/// <inheritdoc />
9183
public IAndWithDbContextTestBuilder WithSet<TDbContext, TEntity>(Func<DbSet<TEntity>, bool> predicate)
92-
where TDbContext : DbContext
84+
where TDbContext : class
9385
where TEntity : class
9486
{
9587
CommonValidator.CheckForNullReference(predicate, nameof(predicate));
9688

97-
if (!predicate(this.TestContext.GetDbContext<TDbContext>().Set<TEntity>()))
89+
if (!predicate(this.TestContext.GetBaseDbContext<TDbContext>().Set<TEntity>()))
9890
{
9991
throw new DataProviderAssertionException(string.Format(
10092
"{0} the {1} set of {2} to pass the given predicate, but it failed.",

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithoutDbContextBuilder.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ public IAndWithoutDbContextBuilder WithoutEntityByKey<TEntity>(object key)
3030

3131
/// <inheritdoc />
3232
public IAndWithoutDbContextBuilder WithoutEntityByKey<TDbContext, TEntity>(object key)
33-
where TDbContext : DbContext
33+
where TDbContext : class
3434
{
35-
var dbContext = this.TestContext.GetDbContext<TDbContext>();
35+
var dbContext = this.TestContext.GetBaseDbContext<TDbContext>();
3636

3737
var entity = dbContext.Find(typeof(TEntity), key);
3838
if (entity == null)
39+
{
3940
return this;
41+
}
4042

4143
dbContext.Remove(entity);
4244
dbContext.SaveChanges();
@@ -51,17 +53,18 @@ public IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TEntity>(IEnumerable<ob
5153

5254
/// <inheritdoc />
5355
public IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TDbContext, TEntity>(IEnumerable<object> keys)
54-
where TDbContext : DbContext
56+
where TDbContext : class
5557
{
56-
var dbContext = this.TestContext.GetDbContext<TDbContext>();
58+
var dbContext = this.TestContext.GetBaseDbContext<TDbContext>();
5759

58-
var entityType = typeof(TEntity);
5960
var entities = keys
60-
.Select(key => dbContext.Find(entityType, key))
61+
.Select(key => dbContext.Find(typeof(TEntity), key))
6162
.Where(entity => entity != null);
6263

6364
if (entities.Any() == false)
65+
{
6466
return this;
67+
}
6568

6669
dbContext.RemoveRange(entities);
6770
dbContext.SaveChanges();
@@ -75,7 +78,7 @@ public IAndWithoutDbContextBuilder WithoutEntity(object entity)
7578

7679
/// <inheritdoc />
7780
public IAndWithoutDbContextBuilder WithoutEntity<TDbContext>(object entity)
78-
where TDbContext : DbContext
81+
where TDbContext : class
7982
=> this.WithoutEntities<TDbContext>(entity);
8083

8184
/// <inheritdoc />
@@ -84,16 +87,16 @@ public IAndWithoutDbContextBuilder WithoutEntities(IEnumerable<object> entities)
8487

8588
/// <inheritdoc />
8689
public IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(IEnumerable<object> entities)
87-
where TDbContext : DbContext
88-
=> this.WithoutEntities<TDbContext>(dbContext => dbContext.RemoveRange(entities));
90+
where TDbContext : class
91+
=> this.WithoutEntities<TDbContext>(dbContext => (dbContext as DbContext).RemoveRange(entities));
8992

9093
/// <inheritdoc />
9194
public IAndWithoutDbContextBuilder WithoutEntities(params object[] entities)
9295
=> this.WithoutEntities(entities.AsEnumerable());
9396

9497
/// <inheritdoc />
9598
public IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(params object[] entities)
96-
where TDbContext : DbContext
99+
where TDbContext : class
97100
=> this.WithoutEntities<TDbContext>(entities.AsEnumerable());
98101

99102
/// <inheritdoc />
@@ -105,20 +108,21 @@ public IAndWithoutDbContextBuilder WithoutAllEntities()
105108
=> this.WithoutEntities<DbContext>(dbContext => dbContext.Database.EnsureDeleted());
106109

107110
/// <inheritdoc />
108-
public IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(Action<TDbContext> dbContextSetup) where TDbContext : DbContext
111+
public IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(Action<TDbContext> dbContextSetup) where TDbContext : class
109112
{
110113
CommonValidator.CheckForNullReference(dbContextSetup, nameof(dbContextSetup));
111114

112115
var dbContext = this.TestContext.GetDbContext<TDbContext>();
116+
113117
dbContextSetup(dbContext);
114118

115119
try
116120
{
117-
dbContext.SaveChanges();
121+
(dbContext as DbContext).SaveChanges();
118122
}
119123
catch (DbUpdateConcurrencyException)
120124
{
121-
// Intentional silent fail, when deleting entities that does not exist in the database or have been already deleted.
125+
// Intentional silent fail, when deleting entities that do not exist in the database or have been already deleted.
122126
}
123127

124128
return this;
@@ -131,12 +135,13 @@ public IAndWithoutDbContextBuilder WithoutSet<TEntity>(Action<DbSet<TEntity>> en
131135

132136
/// <inheritdoc />
133137
public IAndWithoutDbContextBuilder WithoutSet<TDbContext, TEntity>(Action<DbSet<TEntity>> entitySetup)
134-
where TDbContext : DbContext
138+
where TDbContext : class
135139
where TEntity : class
136140
{
137141
CommonValidator.CheckForNullReference(entitySetup, nameof(entitySetup));
138142

139-
var dbContext = this.TestContext.GetDbContext<TDbContext>();
143+
var dbContext = this.TestContext.GetBaseDbContext<TDbContext>();
144+
140145
entitySetup(dbContext.Set<TEntity>());
141146

142147
try
@@ -145,7 +150,7 @@ public IAndWithoutDbContextBuilder WithoutSet<TDbContext, TEntity>(Action<DbSet<
145150
}
146151
catch (DbUpdateConcurrencyException)
147152
{
148-
// Intentional silent fail, when deleting entities that does not exist in the database or have been already deleted.
153+
// Intentional silent fail, when deleting entities that do not exist in the database or have been already deleted.
149154
}
150155

151156
return this;

0 commit comments

Comments
 (0)