mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-13 12:15:46 +08:00
refactor IntegrationTestFixture
This commit is contained in:
parent
3d3f46f0b1
commit
b144ac61a1
@ -1,71 +0,0 @@
|
||||
using System;
|
||||
using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Web;
|
||||
using MassTransit;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute;
|
||||
using Respawn;
|
||||
|
||||
namespace Integration.Test;
|
||||
|
||||
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
public Checkpoint Checkpoint { get; set; }
|
||||
public IConfiguration Configuration => Services.GetRequiredService<IConfiguration>();
|
||||
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
//https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests#set-the-environment
|
||||
//https://stackoverflow.com/questions/43927955/should-getenvironmentvariable-work-in-xunit-test/43951218
|
||||
|
||||
//we could read env from our test launch setting or we can set it directly here
|
||||
builder.UseEnvironment("test");
|
||||
|
||||
//The test app's builder.ConfigureTestServices callback is executed after the app's Startup.ConfigureServices code is executed.
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(IHostedService));
|
||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
services.AddMassTransitTestHarness(x =>
|
||||
{
|
||||
x.UsingRabbitMq((context, cfg) =>
|
||||
{
|
||||
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
|
||||
var host = rabbitMqOptions.HostName;
|
||||
|
||||
cfg.Host(host, h =>
|
||||
{
|
||||
h.Username(rabbitMqOptions.UserName);
|
||||
h.Password(rabbitMqOptions.Password);
|
||||
});
|
||||
cfg.ConfigureEndpoints(context);
|
||||
});
|
||||
});
|
||||
|
||||
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
|
||||
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
});
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
{
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider};
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
|
||||
return httpContextAccessorMock;
|
||||
}
|
||||
}
|
||||
@ -3,11 +3,21 @@ using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Booking.Data;
|
||||
using BuildingBlocks.Domain.Model;
|
||||
using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Web;
|
||||
using Grpc.Net.Client;
|
||||
using MassTransit;
|
||||
using MassTransit.Testing;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute;
|
||||
using Respawn;
|
||||
using Serilog;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
@ -21,29 +31,55 @@ public class FixtureCollection : ICollectionFixture<IntegrationTestFixture>
|
||||
|
||||
public class IntegrationTestFixture : IAsyncLifetime
|
||||
{
|
||||
private readonly CustomWebApplicationFactory _factory;
|
||||
|
||||
public IntegrationTestFixture()
|
||||
{
|
||||
// Ref: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory
|
||||
_factory = new CustomWebApplicationFactory();
|
||||
}
|
||||
|
||||
private WebApplicationFactory<Program> _factory;
|
||||
public Checkpoint Checkpoint { get; set; }
|
||||
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
|
||||
public IServiceProvider ServiceProvider => _factory.Services;
|
||||
public IConfiguration Configuration => _factory.Configuration;
|
||||
public IConfiguration Configuration => _factory.Services.GetRequiredService<IConfiguration>();
|
||||
public HttpClient HttpClient => _factory.CreateClient();
|
||||
public ITestHarness TestHarness => CreateHarness();
|
||||
public GrpcChannel Channel => CreateChannel();
|
||||
|
||||
public virtual Task InitializeAsync()
|
||||
{
|
||||
_factory = new WebApplicationFactory<Program>()
|
||||
.WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.UseEnvironment("test");
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(IHostedService));
|
||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
services.AddMassTransitTestHarness(x =>
|
||||
{
|
||||
x.UsingRabbitMq((context, cfg) =>
|
||||
{
|
||||
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
|
||||
var host = rabbitMqOptions.HostName;
|
||||
|
||||
cfg.Host(host, h =>
|
||||
{
|
||||
h.Username(rabbitMqOptions.UserName);
|
||||
h.Password(rabbitMqOptions.Password);
|
||||
});
|
||||
cfg.ConfigureEndpoints(context);
|
||||
});
|
||||
});
|
||||
|
||||
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
|
||||
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
});
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual async Task DisposeAsync()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Configuration?.GetConnectionString("DefaultConnection")))
|
||||
await _factory.Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
|
||||
await Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
|
||||
|
||||
await _factory.DisposeAsync();
|
||||
}
|
||||
@ -61,7 +97,7 @@ public class IntegrationTestFixture : IAsyncLifetime
|
||||
|
||||
public void RegisterTestServices(Action<IServiceCollection> services)
|
||||
{
|
||||
_factory.TestRegistrationServices = services;
|
||||
TestRegistrationServices = services;
|
||||
}
|
||||
|
||||
public async Task ExecuteScopeAsync(Func<IServiceProvider, Task> action)
|
||||
@ -212,4 +248,16 @@ public class IntegrationTestFixture : IAsyncLifetime
|
||||
{
|
||||
return GrpcChannel.ForAddress(HttpClient.BaseAddress!, new GrpcChannelOptions {HttpClient = HttpClient});
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
{
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider};
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
|
||||
return httpContextAccessorMock;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,71 +0,0 @@
|
||||
using System;
|
||||
using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Web;
|
||||
using MassTransit;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute;
|
||||
using Respawn;
|
||||
|
||||
namespace Integration.Test;
|
||||
|
||||
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
public Checkpoint Checkpoint { get; set; }
|
||||
public IConfiguration Configuration => Services.GetRequiredService<IConfiguration>();
|
||||
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
//https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests#set-the-environment
|
||||
//https://stackoverflow.com/questions/43927955/should-getenvironmentvariable-work-in-xunit-test/43951218
|
||||
|
||||
//we could read env from our test launch setting or we can set it directly here
|
||||
builder.UseEnvironment("test");
|
||||
|
||||
//The test app's builder.ConfigureTestServices callback is executed after the app's Startup.ConfigureServices code is executed.
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(IHostedService));
|
||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
services.AddMassTransitTestHarness(x =>
|
||||
{
|
||||
x.UsingRabbitMq((context, cfg) =>
|
||||
{
|
||||
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
|
||||
var host = rabbitMqOptions.HostName;
|
||||
|
||||
cfg.Host(host, h =>
|
||||
{
|
||||
h.Username(rabbitMqOptions.UserName);
|
||||
h.Password(rabbitMqOptions.Password);
|
||||
});
|
||||
cfg.ConfigureEndpoints(context);
|
||||
});
|
||||
});
|
||||
|
||||
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
|
||||
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
});
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
{
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider};
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
|
||||
return httpContextAccessorMock;
|
||||
}
|
||||
}
|
||||
@ -3,15 +3,24 @@ using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using BuildingBlocks.Domain.Model;
|
||||
using BuildingBlocks.EFCore;
|
||||
using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Web;
|
||||
using Flight.Data;
|
||||
using FluentAssertions.Common;
|
||||
using Grpc.Net.Client;
|
||||
using MassTransit;
|
||||
using MassTransit.Testing;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute;
|
||||
using Respawn;
|
||||
using Serilog;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
@ -19,20 +28,17 @@ using Xunit.Abstractions;
|
||||
namespace Integration.Test;
|
||||
|
||||
[CollectionDefinition(nameof(IntegrationTestFixture))]
|
||||
public class FixtureCollection : ICollectionFixture<IntegrationTestFixture> { }
|
||||
public class FixtureCollection : ICollectionFixture<IntegrationTestFixture>
|
||||
{
|
||||
}
|
||||
|
||||
public class IntegrationTestFixture : IAsyncLifetime
|
||||
{
|
||||
private readonly CustomWebApplicationFactory _factory;
|
||||
|
||||
public IntegrationTestFixture()
|
||||
{
|
||||
// Ref: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory
|
||||
_factory = new CustomWebApplicationFactory();
|
||||
}
|
||||
|
||||
private WebApplicationFactory<Program> _factory;
|
||||
public Checkpoint Checkpoint { get; set; }
|
||||
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
|
||||
public IServiceProvider ServiceProvider => _factory.Services;
|
||||
public IConfiguration Configuration => _factory.Configuration;
|
||||
public IConfiguration Configuration => _factory.Services.GetRequiredService<IConfiguration>();
|
||||
public HttpClient HttpClient => _factory.CreateClient();
|
||||
public ITestHarness TestHarness => CreateHarness();
|
||||
public GrpcChannel Channel => CreateChannel();
|
||||
@ -50,17 +56,48 @@ public class IntegrationTestFixture : IAsyncLifetime
|
||||
return null;
|
||||
}
|
||||
|
||||
public void RegisterTestServices(Action<IServiceCollection> services) => _factory.TestRegistrationServices = services;
|
||||
public void RegisterTestServices(Action<IServiceCollection> services) => TestRegistrationServices = services;
|
||||
|
||||
public virtual Task InitializeAsync()
|
||||
{
|
||||
_factory = new WebApplicationFactory<Program>()
|
||||
.WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.UseEnvironment("test");
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(IHostedService));
|
||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
services.AddMassTransitTestHarness(x =>
|
||||
{
|
||||
x.UsingRabbitMq((context, cfg) =>
|
||||
{
|
||||
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
|
||||
var host = rabbitMqOptions.HostName;
|
||||
|
||||
cfg.Host(host, h =>
|
||||
{
|
||||
h.Username(rabbitMqOptions.UserName);
|
||||
h.Password(rabbitMqOptions.Password);
|
||||
});
|
||||
cfg.ConfigureEndpoints(context);
|
||||
});
|
||||
});
|
||||
|
||||
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
|
||||
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
});
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual async Task DisposeAsync()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Configuration?.GetConnectionString("DefaultConnection")))
|
||||
await _factory.Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
|
||||
await Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
|
||||
|
||||
await _factory.DisposeAsync();
|
||||
}
|
||||
@ -213,4 +250,16 @@ public class IntegrationTestFixture : IAsyncLifetime
|
||||
{
|
||||
return GrpcChannel.ForAddress(HttpClient.BaseAddress!, new GrpcChannelOptions {HttpClient = HttpClient});
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
{
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider};
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
|
||||
return httpContextAccessorMock;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,71 +0,0 @@
|
||||
using System;
|
||||
using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Web;
|
||||
using MassTransit;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute;
|
||||
using Respawn;
|
||||
|
||||
namespace Integration.Test;
|
||||
|
||||
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
public Checkpoint Checkpoint { get; set; }
|
||||
public IConfiguration Configuration => Services.GetRequiredService<IConfiguration>();
|
||||
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
//https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests#set-the-environment
|
||||
//https://stackoverflow.com/questions/43927955/should-getenvironmentvariable-work-in-xunit-test/43951218
|
||||
|
||||
//we could read env from our test launch setting or we can set it directly here
|
||||
builder.UseEnvironment("test");
|
||||
|
||||
//The test app's builder.ConfigureTestServices callback is executed after the app's Startup.ConfigureServices code is executed.
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(IHostedService));
|
||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
services.AddMassTransitTestHarness(x =>
|
||||
{
|
||||
x.UsingRabbitMq((context, cfg) =>
|
||||
{
|
||||
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
|
||||
var host = rabbitMqOptions.HostName;
|
||||
|
||||
cfg.Host(host, h =>
|
||||
{
|
||||
h.Username(rabbitMqOptions.UserName);
|
||||
h.Password(rabbitMqOptions.Password);
|
||||
});
|
||||
cfg.ConfigureEndpoints(context);
|
||||
});
|
||||
});
|
||||
|
||||
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
|
||||
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
});
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
{
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider};
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
|
||||
return httpContextAccessorMock;
|
||||
}
|
||||
}
|
||||
@ -7,10 +7,10 @@
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.4"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0"/>
|
||||
<PackageReference Include="xunit" Version="2.4.1"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
@ -22,7 +22,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Identity.Api\Identity.Api.csproj"/>
|
||||
<ProjectReference Include="..\..\src\Identity.Api\Identity.Api.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -2,16 +2,22 @@
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using BuildingBlocks.Domain.Model;
|
||||
using BuildingBlocks.EFCore;
|
||||
using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Web;
|
||||
using Grpc.Net.Client;
|
||||
using Identity.Data;
|
||||
using MassTransit;
|
||||
using MassTransit.Testing;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute;
|
||||
using Respawn;
|
||||
using Serilog;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
@ -19,52 +25,81 @@ using Xunit.Abstractions;
|
||||
namespace Integration.Test;
|
||||
|
||||
[CollectionDefinition(nameof(IntegrationTestFixture))]
|
||||
public class FixtureCollection : ICollectionFixture<IntegrationTestFixture> { }
|
||||
public class FixtureCollection : ICollectionFixture<IntegrationTestFixture>
|
||||
{
|
||||
}
|
||||
|
||||
public class IntegrationTestFixture : IAsyncLifetime
|
||||
{
|
||||
private readonly CustomWebApplicationFactory _factory;
|
||||
|
||||
public IntegrationTestFixture()
|
||||
{
|
||||
// Ref: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory
|
||||
_factory = new CustomWebApplicationFactory();
|
||||
}
|
||||
|
||||
private WebApplicationFactory<Program> _factory;
|
||||
public Checkpoint Checkpoint { get; set; }
|
||||
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
|
||||
public IServiceProvider ServiceProvider => _factory.Services;
|
||||
public IConfiguration Configuration => _factory.Configuration;
|
||||
public IConfiguration Configuration => _factory.Services.GetRequiredService<IConfiguration>();
|
||||
public HttpClient HttpClient => _factory.CreateClient();
|
||||
public ITestHarness TestHarness => CreateHarness();
|
||||
public GrpcChannel Channel => CreateChannel();
|
||||
|
||||
// ref: https://github.com/trbenning/serilog-sinks-xunit
|
||||
public ILogger CreateLogger(ITestOutputHelper output)
|
||||
{
|
||||
if (output != null)
|
||||
{
|
||||
return new LoggerConfiguration()
|
||||
.WriteTo.TestOutput(output)
|
||||
.CreateLogger();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void RegisterTestServices(Action<IServiceCollection> services) => _factory.TestRegistrationServices = services;
|
||||
|
||||
public virtual Task InitializeAsync()
|
||||
{
|
||||
_factory = new WebApplicationFactory<Program>()
|
||||
.WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.UseEnvironment("test");
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(IHostedService));
|
||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
services.AddMassTransitTestHarness(x =>
|
||||
{
|
||||
x.UsingRabbitMq((context, cfg) =>
|
||||
{
|
||||
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
|
||||
var host = rabbitMqOptions.HostName;
|
||||
|
||||
cfg.Host(host, h =>
|
||||
{
|
||||
h.Username(rabbitMqOptions.UserName);
|
||||
h.Password(rabbitMqOptions.Password);
|
||||
});
|
||||
cfg.ConfigureEndpoints(context);
|
||||
});
|
||||
});
|
||||
|
||||
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
|
||||
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
});
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual async Task DisposeAsync()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Configuration?.GetConnectionString("DefaultConnection")))
|
||||
await _factory.Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
|
||||
await Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
|
||||
|
||||
await _factory.DisposeAsync();
|
||||
}
|
||||
|
||||
// ref: https://github.com/trbenning/serilog-sinks-xunit
|
||||
public ILogger CreateLogger(ITestOutputHelper output)
|
||||
{
|
||||
if (output != null)
|
||||
return new LoggerConfiguration()
|
||||
.WriteTo.TestOutput(output)
|
||||
.CreateLogger();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void RegisterTestServices(Action<IServiceCollection> services)
|
||||
{
|
||||
TestRegistrationServices = services;
|
||||
}
|
||||
|
||||
public async Task ExecuteScopeAsync(Func<IServiceProvider, Task> action)
|
||||
{
|
||||
using var scope = ServiceProvider.CreateScope();
|
||||
@ -213,4 +248,16 @@ public class IntegrationTestFixture : IAsyncLifetime
|
||||
{
|
||||
return GrpcChannel.ForAddress(HttpClient.BaseAddress!, new GrpcChannelOptions {HttpClient = HttpClient});
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
{
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider};
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
|
||||
return httpContextAccessorMock;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,72 +0,0 @@
|
||||
using System;
|
||||
using BuildingBlocks.Contracts.Grpc;
|
||||
using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Web;
|
||||
using MassTransit;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute;
|
||||
using Respawn;
|
||||
|
||||
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
public Checkpoint Checkpoint { get; set; }
|
||||
public IConfiguration Configuration => Services.GetRequiredService<IConfiguration>();
|
||||
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
//https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests#set-the-environment
|
||||
//https://stackoverflow.com/questions/43927955/should-getenvironmentvariable-work-in-xunit-test/43951218
|
||||
|
||||
//we could read env from our test launch setting or we can set it directly here
|
||||
builder.UseEnvironment("test");
|
||||
|
||||
//The test app's builder.ConfigureTestServices callback is executed after the app's Startup.ConfigureServices code is executed.
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(IHostedService));
|
||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
services.AddMassTransitTestHarness(x =>
|
||||
{
|
||||
x.UsingRabbitMq((context, cfg) =>
|
||||
{
|
||||
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
|
||||
var host = rabbitMqOptions.HostName;
|
||||
|
||||
cfg.Host(host, h =>
|
||||
{
|
||||
h.Username(rabbitMqOptions.UserName);
|
||||
h.Password(rabbitMqOptions.Password);
|
||||
});
|
||||
cfg.ConfigureEndpoints(context);
|
||||
});
|
||||
});
|
||||
|
||||
services.ReplaceServiceWithSingletonMock<IPassengerGrpcService>();
|
||||
|
||||
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
|
||||
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
});
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
{
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider};
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
|
||||
return httpContextAccessorMock;
|
||||
}
|
||||
}
|
||||
@ -1,25 +1,23 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
using BuildingBlocks.Domain.Model;
|
||||
using BuildingBlocks.EFCore;
|
||||
using Grpc.Core;
|
||||
using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Web;
|
||||
using Grpc.Net.Client;
|
||||
using MagicOnion;
|
||||
using MagicOnion.Client;
|
||||
using MagicOnion.Server;
|
||||
using MassTransit;
|
||||
using MassTransit.Testing;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute;
|
||||
using Passenger.Data;
|
||||
using Respawn;
|
||||
using Serilog;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
@ -27,52 +25,81 @@ using Xunit.Abstractions;
|
||||
namespace Integration.Test;
|
||||
|
||||
[CollectionDefinition(nameof(IntegrationTestFixture))]
|
||||
public class FixtureCollection : ICollectionFixture<IntegrationTestFixture> { }
|
||||
public class FixtureCollection : ICollectionFixture<IntegrationTestFixture>
|
||||
{
|
||||
}
|
||||
|
||||
public class IntegrationTestFixture : IAsyncLifetime
|
||||
{
|
||||
private readonly CustomWebApplicationFactory _factory;
|
||||
|
||||
public IntegrationTestFixture()
|
||||
{
|
||||
// Ref: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory
|
||||
_factory = new CustomWebApplicationFactory();
|
||||
}
|
||||
|
||||
private WebApplicationFactory<Program> _factory;
|
||||
public Checkpoint Checkpoint { get; set; }
|
||||
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
|
||||
public IServiceProvider ServiceProvider => _factory.Services;
|
||||
public IConfiguration Configuration => _factory.Configuration;
|
||||
public IConfiguration Configuration => _factory.Services.GetRequiredService<IConfiguration>();
|
||||
public HttpClient HttpClient => _factory.CreateClient();
|
||||
public ITestHarness TestHarness => CreateHarness();
|
||||
public GrpcChannel Channel => CreateChannel();
|
||||
|
||||
// ref: https://github.com/trbenning/serilog-sinks-xunit
|
||||
public ILogger CreateLogger(ITestOutputHelper output)
|
||||
{
|
||||
if (output != null)
|
||||
{
|
||||
return new LoggerConfiguration()
|
||||
.WriteTo.TestOutput(output)
|
||||
.CreateLogger();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void RegisterTestServices(Action<IServiceCollection> services) => _factory.TestRegistrationServices = services;
|
||||
|
||||
public virtual Task InitializeAsync()
|
||||
{
|
||||
_factory = new WebApplicationFactory<Program>()
|
||||
.WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.UseEnvironment("test");
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(IHostedService));
|
||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
services.AddMassTransitTestHarness(x =>
|
||||
{
|
||||
x.UsingRabbitMq((context, cfg) =>
|
||||
{
|
||||
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>("RabbitMq");
|
||||
var host = rabbitMqOptions.HostName;
|
||||
|
||||
cfg.Host(host, h =>
|
||||
{
|
||||
h.Username(rabbitMqOptions.UserName);
|
||||
h.Password(rabbitMqOptions.Password);
|
||||
});
|
||||
cfg.ConfigureEndpoints(context);
|
||||
});
|
||||
});
|
||||
|
||||
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
|
||||
|
||||
TestRegistrationServices?.Invoke(services);
|
||||
});
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual async Task DisposeAsync()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Configuration?.GetConnectionString("DefaultConnection")))
|
||||
await _factory.Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
|
||||
await Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
|
||||
|
||||
await _factory.DisposeAsync();
|
||||
}
|
||||
|
||||
// ref: https://github.com/trbenning/serilog-sinks-xunit
|
||||
public ILogger CreateLogger(ITestOutputHelper output)
|
||||
{
|
||||
if (output != null)
|
||||
return new LoggerConfiguration()
|
||||
.WriteTo.TestOutput(output)
|
||||
.CreateLogger();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void RegisterTestServices(Action<IServiceCollection> services)
|
||||
{
|
||||
TestRegistrationServices = services;
|
||||
}
|
||||
|
||||
public async Task ExecuteScopeAsync(Func<IServiceProvider, Task> action)
|
||||
{
|
||||
using var scope = ServiceProvider.CreateScope();
|
||||
@ -221,4 +248,16 @@ public class IntegrationTestFixture : IAsyncLifetime
|
||||
{
|
||||
return GrpcChannel.ForAddress(HttpClient.BaseAddress!, new GrpcChannelOptions {HttpClient = HttpClient});
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
{
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider};
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
|
||||
return httpContextAccessorMock;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user