Skip to content

Commit 4ccc00b

Browse files
committed
Added ShouldHaveTestBuilderEntityFrameworkCoreExtensions with DbContext method (#125)
1 parent 9390cb7 commit 4ccc00b

File tree

13 files changed

+157
-30
lines changed

13 files changed

+157
-30
lines changed

src/MyTested.AspNetCore.Mvc.Core/Builders/Contracts/ActionResults/Created/ICreatedTestBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public interface ICreatedTestBuilder : IBaseTestBuilderWithResponseModel,
2525
IAndCreatedTestBuilder AtLocation(string location);
2626

2727
/// <summary>
28-
/// Tests whether <see cref="CreatedResult"/> location passes given assertions.
28+
/// Tests whether <see cref="CreatedResult"/> location passes the given assertions.
2929
/// </summary>
3030
/// <param name="assertions">Action containing all assertions on the location.</param>
3131
/// <returns>The same <see cref="IAndCreatedTestBuilder"/>.</returns>
3232
IAndCreatedTestBuilder AtLocationPassing(Action<string> assertions);
3333

3434
/// <summary>
35-
/// Tests whether <see cref="CreatedResult"/> location passes given predicate.
35+
/// Tests whether <see cref="CreatedResult"/> location passes the given predicate.
3636
/// </summary>
3737
/// <param name="predicate">Predicate testing the location.</param>
3838
/// <returns>The same <see cref="IAndCreatedTestBuilder"/>.</returns>

src/MyTested.AspNetCore.Mvc.Core/Builders/Contracts/ActionResults/LocalRedirect/ILocalRedirectTestBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ public interface ILocalRedirectTestBuilder : IBaseTestBuilderWithActionResult<Lo
2626
IAndLocalRedirectTestBuilder ToUrl(string localUrl);
2727

2828
/// <summary>
29-
/// Tests whether <see cref="LocalRedirectResult"/> URL passes given assertions.
29+
/// Tests whether <see cref="LocalRedirectResult"/> URL passes the given assertions.
3030
/// </summary>
3131
/// <param name="assertions">Action containing all assertions on the URL.</param>
3232
/// <returns>The same <see cref="IAndLocalRedirectTestBuilder"/>.</returns>
3333
IAndLocalRedirectTestBuilder ToUrlPassing(Action<string> assertions);
3434

3535
/// <summary>
36-
/// Tests whether <see cref="LocalRedirectResult"/> URL passes given predicate.
36+
/// Tests whether <see cref="LocalRedirectResult"/> URL passes the given predicate.
3737
/// </summary>
3838
/// <param name="predicate">Predicate testing the URL.</param>
3939
/// <returns>The same <see cref="IAndLocalRedirectTestBuilder"/>.</returns>

src/MyTested.AspNetCore.Mvc.Core/Builders/Contracts/ActionResults/Redirect/IRedirectTestBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public interface IRedirectTestBuilder : IBaseTestBuilderWithActionResult<ActionR
2727
IAndRedirectTestBuilder ToUrl(string location);
2828

2929
/// <summary>
30-
/// Tests whether <see cref="RedirectResult"/> location passes given assertions.
30+
/// Tests whether <see cref="RedirectResult"/> location passes the given assertions.
3131
/// </summary>
3232
/// <param name="assertions">Action containing all assertions on the location.</param>
3333
/// <returns>The same <see cref="IAndRedirectTestBuilder"/>.</returns>
3434
IAndRedirectTestBuilder ToUrlPassing(Action<string> assertions);
3535

3636
/// <summary>
37-
/// Tests whether <see cref="RedirectResult"/> location passes given predicate.
37+
/// Tests whether <see cref="RedirectResult"/> location passes the given predicate.
3838
/// </summary>
3939
/// <param name="predicate">Predicate testing the location.</param>
4040
/// <returns>The same <see cref="IAndRedirectTestBuilder"/>.</returns>

src/MyTested.AspNetCore.Mvc.Core/Builders/Contracts/Actions/IShouldReturnTestBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ public interface IShouldReturnTestBuilder<TActionResult> : IBaseTestBuilderWithA
8383
IContentTestBuilder Content(string content);
8484

8585
/// <summary>
86-
/// Tests whether <see cref="Microsoft.AspNetCore.Mvc.ContentResult"/> passes given assertions.
86+
/// Tests whether <see cref="Microsoft.AspNetCore.Mvc.ContentResult"/> passes the given assertions.
8787
/// </summary>
8888
/// <param name="assertions">Action containing all assertions on the content.</param>
8989
/// <returns>Test builder of <see cref="IContentTestBuilder"/> type.</returns>
9090
IContentTestBuilder Content(Action<string> assertions);
9191

9292
/// <summary>
93-
/// Tests whether <see cref="Microsoft.AspNetCore.Mvc.ContentResult"/> passes given predicate.
93+
/// Tests whether <see cref="Microsoft.AspNetCore.Mvc.ContentResult"/> passes the given predicate.
9494
/// </summary>
9595
/// <param name="predicate">Predicate testing the content.</param>
9696
/// <returns>Test builder of <see cref="IContentTestBuilder"/> type.</returns>

src/MyTested.AspNetCore.Mvc.Core/Builders/Contracts/Models/IModelDetailsTestBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
public interface IModelDetailsTestBuilder<TResponseModel> : IModelErrorTestBuilder<TResponseModel>
1010
{
1111
/// <summary>
12-
/// Tests whether the returned model from the invoked action passes given assertions.
12+
/// Tests whether the returned model from the invoked action passes the given assertions.
1313
/// </summary>
1414
/// <param name="assertions">Action containing all assertions on the model.</param>
1515
/// <returns>Test builder of <see cref="IModelErrorTestBuilder{TResponseModel}"/>.</returns>
1616
IModelErrorTestBuilder<TResponseModel> Passing(Action<TResponseModel> assertions);
1717

1818
/// <summary>
19-
/// Tests whether the returned model from the invoked action passes given predicate.
19+
/// Tests whether the returned model from the invoked action passes the given predicate.
2020
/// </summary>
2121
/// <param name="predicate">Predicate testing the model.</param>
2222
/// <returns>Test builder of <see cref="IModelErrorTestBuilder{TResponseModel}"/>.</returns>

src/MyTested.AspNetCore.Mvc.Core/Builders/Controllers/ControllerServiceBuilder.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Collections.Generic;
55
using Contracts.Controllers;
6-
using Internal.Application;
76
using Microsoft.Extensions.DependencyInjection;
87
using Utilities;
98
using Utilities.Extensions;
@@ -18,13 +17,8 @@ public partial class ControllerBuilder<TController>
1817
public IAndControllerBuilder<TController> WithServiceSetupFor<TService>(Action<TService> scopedServiceSetup)
1918
{
2019
CommonValidator.CheckForNullReference(scopedServiceSetup, nameof(scopedServiceSetup));
21-
22-
var serviceLifetime = TestServiceProvider.GetServiceLifetime<TService>();
23-
if (serviceLifetime != ServiceLifetime.Scoped)
24-
{
25-
throw new InvalidOperationException("The 'WithServiceSetupFor' method can be used only for services with scoped lifetime.");
26-
}
27-
20+
ServiceValidator.ValidateScopedServiceLifetime<TService>(nameof(WithServiceSetupFor));
21+
2822
scopedServiceSetup(this.HttpContext.RequestServices.GetService<TService>());
2923

3024
return this;

src/MyTested.AspNetCore.Mvc.Core/Internal/Application/TestServiceProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static TInstance GetService<TInstance>()
5858
ServiceValidator.ValidateServices();
5959
return Current.GetService<TInstance>();
6060
}
61-
61+
6262
/// <summary>
6363
/// Gets collection of services. Returns null if no service of this type is not found. Throws exception if there are no registered services.
6464
/// </summary>

src/MyTested.AspNetCore.Mvc.Core/Utilities/Validators/ServiceValidator.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace MyTested.AspNetCore.Mvc.Utilities.Validators
22
{
3+
using System;
34
using Internal.Application;
5+
using Microsoft.Extensions.DependencyInjection;
46

57
/// <summary>
68
/// Validator class containing application services validation logic.
@@ -29,6 +31,20 @@ public static void ValidateServiceExists<TService>(TService service)
2931
$"{typeof(TService).Name} could not be resolved from the services provider. Before running this test case, the service should be registered in the 'StartsFrom' method and");
3032
}
3133

34+
/// <summary>
35+
/// Validates whether service exists and has scoped <see cref="ServiceLifetime"/>.
36+
/// </summary>
37+
/// <typeparam name="TService">Type of service to validate.</typeparam>
38+
/// <param name="methodName">Name of the calling method.</param>
39+
public static void ValidateScopedServiceLifetime<TService>(string methodName)
40+
{
41+
var serviceLifetime = TestServiceProvider.GetServiceLifetime<TService>();
42+
if (serviceLifetime != ServiceLifetime.Scoped)
43+
{
44+
throw new InvalidOperationException($"The '{methodName}' method can be used only for services with scoped lifetime.");
45+
}
46+
}
47+
3248
/// <summary>
3349
/// Validates whether formatter exists.
3450
/// </summary>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
2+
{
3+
using System;
4+
using Microsoft.EntityFrameworkCore;
5+
6+
/// <summary>
7+
/// Used for testing <see cref="DbContext"/>.
8+
/// </summary>
9+
public interface IDbContextTestBuilder
10+
{
11+
/// <summary>
12+
/// Tests whether <see cref="DbContext"/> entries pass the given assertions.
13+
/// </summary>
14+
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/>.</typeparam>
15+
/// <param name="assertions">Action containing all assertions on the <see cref="DbContext"/> entities.</param>
16+
void WithEntries<TDbContext>(Action<TDbContext> assertions)
17+
where TDbContext : DbContext;
18+
19+
/// <summary>
20+
/// Tests whether <see cref="DbContext"/> entries pass the given predicate.
21+
/// </summary>
22+
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/>.</typeparam>
23+
/// <param name="predicate">Predicate testing the <see cref="DbContext"/> entities.</param>
24+
void WithEntries<TDbContext>(Func<TDbContext, bool> predicate)
25+
where TDbContext : DbContext;
26+
}
27+
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using Base;
55
using Contracts.Data;
6-
using Internal.Application;
76
using Internal.TestContexts;
87
using Microsoft.EntityFrameworkCore;
98
using Microsoft.Extensions.DependencyInjection;
@@ -28,17 +27,14 @@ public void WithSetup<TDbContext>(Action<TDbContext> dbContextSetup)
2827
where TDbContext : DbContext
2928
{
3029
CommonValidator.CheckForNullReference(dbContextSetup, nameof(dbContextSetup));
31-
32-
var serviceLifetime = TestServiceProvider.GetServiceLifetime<TDbContext>();
33-
if (serviceLifetime != ServiceLifetime.Scoped)
34-
{
35-
throw new InvalidOperationException("The 'WithSetup' method can be used only for services with scoped lifetime.");
36-
}
37-
38-
var dbContext = this.TestContext.HttpContext.RequestServices.GetRequiredService<TDbContext>();
30+
ServiceValidator.ValidateScopedServiceLifetime<TDbContext>(nameof(WithSetup));
31+
32+
var dbContext = this.TestContext
33+
.HttpContext
34+
.RequestServices
35+
.GetRequiredService<TDbContext>();
3936

4037
dbContextSetup(dbContext);
41-
4238
dbContext.SaveChanges();
4339
}
4440
}

0 commit comments

Comments
 (0)