mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-03 16:19:46 +00:00
48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Mongo2Go;
|
|
using ShoppingAssistantApi.Persistance.PersistanceExtentions;
|
|
|
|
namespace ShoppingAssistantApi.Tests.TestExtentions;
|
|
|
|
public class TestingFactory<TEntryPoint> : WebApplicationFactory<Program> where TEntryPoint : Program
|
|
{
|
|
private readonly MongoDbRunner _runner = MongoDbRunner.Start();
|
|
|
|
private bool _isDataInitialaized = false;
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
builder.ConfigureAppConfiguration((context, config) =>
|
|
{
|
|
var dbConfig = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string>()
|
|
{
|
|
{ "ConnectionStrings:MongoDb", _runner.ConnectionString }
|
|
})
|
|
.Build();
|
|
|
|
config.AddConfiguration(dbConfig);
|
|
});
|
|
}
|
|
|
|
public async Task InitialaizeData()
|
|
{
|
|
if (!_isDataInitialaized)
|
|
{
|
|
_isDataInitialaized = true;
|
|
using var scope = Services.CreateScope();
|
|
var initialaizer = new DbInitialaizer(scope.ServiceProvider);
|
|
using var cancellationTokenSource = new CancellationTokenSource();
|
|
var cancellationToken = cancellationTokenSource.Token;
|
|
await initialaizer.InitialaizeDb(cancellationToken);
|
|
}
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
_runner.Dispose();
|
|
base.Dispose(disposing);
|
|
}
|
|
} |