Skip to content

Commit e0e4208

Browse files
committed
test: mock WebApplicationFactory
1 parent 06669b8 commit e0e4208

5 files changed

Lines changed: 48 additions & 7 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"EnvironmentTesting": true,
3+
"Logging": {
4+
"LogLevel": {
5+
"Default": "Information",
6+
"Microsoft.AspNetCore": "Warning"
7+
}
8+
},
9+
"Settings": {
10+
"key": "dot"
11+
}
12+
}

test/API.Test/User/Register/RegisterUserEndpoint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
using System.Net.Http.Json;
33
using System.Text.Json;
44
using CommonTestUtilities.Requests;
5-
using Microsoft.AspNetCore.Mvc.Testing;
5+
using CommonTestUtilities.Services;
66

77
namespace 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

test/CommonTestUtilities/Services/EncryptMockFactory.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace 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
}

test/CommonTestUtilities/Services/MapperMockFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace CommonTestUtilities.Services;
55

6-
public class MapperMockFactory
6+
public static class MapperMockFactory
77
{
88
public static IMapper CreateMock()
99
{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)