File tree Expand file tree Collapse file tree
src/BackEnd/RecipesApp.API
CommonTestUtilities/Services Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ {
2+ "EnvironmentTesting" : true ,
3+ "Logging" : {
4+ "LogLevel" : {
5+ "Default" : " Information" ,
6+ "Microsoft.AspNetCore" : " Warning"
7+ }
8+ },
9+ "Settings" : {
10+ "key" : " dot"
11+ }
12+ }
Original file line number Diff line number Diff line change 22using System . Net . Http . Json ;
33using System . Text . Json ;
44using CommonTestUtilities . Requests ;
5- using Microsoft . AspNetCore . Mvc . Testing ;
5+ using CommonTestUtilities . Services ;
66
77namespace API . Test . User . Register ;
88
9- public class RegisterUserEndpoint ( WebApplicationFactory < Program > factory )
10- : IClassFixture < WebApplicationFactory < Program > >
9+ public class RegisterUserEndpoint ( WebApplicationMockFactory factory )
10+ : IClassFixture < WebApplicationMockFactory >
1111{
1212 private readonly HttpClient _client = factory . CreateClient ( ) ;
1313
Original file line number Diff line number Diff line change 22
33namespace CommonTestUtilities . Services ;
44
5- public class EncryptMockFactory
5+ public static class EncryptMockFactory
66{
7- public static PasswordEncryptionService CreateMock ( )
8- => new PasswordEncryptionService ( "dot" ) ;
7+ public static PasswordEncryptionService CreateMock ( ) => new ( "dot" ) ;
98}
Original file line number Diff line number Diff line change 33
44namespace CommonTestUtilities . Services ;
55
6- public class MapperMockFactory
6+ public static class MapperMockFactory
77{
88 public static IMapper CreateMock ( )
99 {
Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Hosting ;
2+ using Microsoft . AspNetCore . Mvc . Testing ;
3+ using Microsoft . EntityFrameworkCore ;
4+ using Microsoft . Extensions . DependencyInjection ;
5+ using RecipesApp . Infra . Data ;
6+
7+ namespace CommonTestUtilities . Services ;
8+
9+ public class WebApplicationMockFactory : WebApplicationFactory < Program >
10+ {
11+ protected override void ConfigureWebHost ( IWebHostBuilder builder )
12+ {
13+ builder . UseEnvironment ( "Testing" )
14+ . ConfigureServices ( services =>
15+ {
16+ var descriptor = services . SingleOrDefault ( d => d . ServiceType == typeof ( DbContextOptions < RecipesAppContext > ) ) ;
17+
18+ if ( descriptor is not null )
19+ services . Remove ( descriptor ) ;
20+ var provider = services . AddEntityFrameworkInMemoryDatabase ( )
21+ . BuildServiceProvider ( ) ;
22+
23+ services . AddDbContext < RecipesAppContext > ( options =>
24+ {
25+ options . UseInMemoryDatabase ( "InMemoryDatabaseForTesting" ) ;
26+ options . UseInternalServiceProvider ( provider ) ;
27+ } ) ;
28+ } ) ;
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments