-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebApplicationFixture.cs
More file actions
56 lines (43 loc) · 1.96 KB
/
WebApplicationFixture.cs
File metadata and controls
56 lines (43 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace Comanda.Stores.TestSuite.Fixtures;
public sealed class WebApplicationFixture : IAsyncLifetime
{
private readonly MongoDatabaseFixture _databaseFixture;
public HttpClient HttpClient { get; private set; } = default!;
public IServiceProvider Services { get; private set; } = default!;
private WebApplicationFactory<Program> _factory = default!;
public WebApplicationFixture()
{
_databaseFixture = new MongoDatabaseFixture();
}
public async ValueTask InitializeAsync()
{
await _databaseFixture.InitializeAsync();
await _databaseFixture.CleanDatabaseAsync();
Environment.SetEnvironmentVariable("Settings__Database__ConnectionString", _databaseFixture.ConnectionString);
Environment.SetEnvironmentVariable("Settings__Database__DatabaseName", _databaseFixture.DatabaseName);
_factory = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
services.AddBypassAuthentication();
var descriptor = services.SingleOrDefault(descriptor => descriptor.ServiceType == typeof(IMongoClient));
if (descriptor is not null)
services.Remove(descriptor);
descriptor = services.SingleOrDefault(descriptor => descriptor.ServiceType == typeof(IMongoDatabase));
if (descriptor is not null)
services.Remove(descriptor);
services.AddSingleton(_ => _databaseFixture.Client);
services.AddSingleton(_ => _databaseFixture.Database);
});
});
HttpClient = _factory.CreateClient();
Services = _factory.Services;
}
public async ValueTask DisposeAsync()
{
HttpClient.Dispose();
await _factory.DisposeAsync();
await _databaseFixture.DisposeAsync();
}
}