Created
May 12, 2022 15:20
-
-
Save mrpmorris/0b048c7a791eb29df06f526ec944b56a to your computer and use it in GitHub Desktop.
ASP.NET integration testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Whatever | |
public static class IntegrationTestsServer | |
{ | |
public static string? IdentityConfirmationCode { get; private set; } | |
public static string? SignInOneTimeCode { get; private set; } | |
public static IOptions<GameServerOptions> GameServerOptions { get; set; } | |
private static readonly HttpClient HttpClient; | |
private static readonly IConfiguration Configuration; | |
private static Mock<IUserMessagingService> MockUserMessagingService = null!; | |
static IntegrationTestsServer() | |
{ | |
ConfigureMocks(); | |
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "IntegrationTesting"); | |
var appBuilder = new WebApplicationFactory<Program>() | |
.WithWebHostBuilder(builder => | |
{ | |
builder.ConfigureTestServices(services => | |
{ | |
services.AddScoped(sp => MockUserMessagingService.Object); | |
}); | |
}); | |
Configuration = appBuilder.Services.GetRequiredService<IConfiguration>(); | |
GameServerOptions = appBuilder.Services.GetRequiredService<IOptions<GameServerOptions>>(); | |
var dbContextOptions = appBuilder.Services.GetRequiredService<DbContextOptions<ApplicationDbContext>>(); | |
using var dbContext = new ApplicationDbContext(dbContextOptions); | |
dbContext.Database.EnsureDeleted(); | |
dbContext.Database.EnsureCreated(); | |
HttpClient = appBuilder.CreateClient(); | |
} | |
public static async Task<TResponse> PostAsync<TResponse>(string url, IRequest<TResponse> request) | |
where TResponse : Response, new() | |
{ | |
ArgumentNullException.ThrowIfNull(url); | |
ArgumentNullException.ThrowIfNull(request); | |
string requestJson = JsonConvert.SerializeObject(request); | |
var httpRequest = new StringContent(requestJson); | |
httpRequest.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); | |
HttpResponseMessage httpResponse = await HttpClient.PostAsync(url, httpRequest); | |
string json = await httpResponse.Content.ReadAsStringAsync(); | |
return JsonConvert.DeserializeObject<TResponse>(json)!; | |
} | |
private static void ConfigureMocks() | |
{ | |
MockUserMessagingService = new Mock<IUserMessagingService>(); | |
MockUserMessagingService | |
.Setup(x => x.SendNewUserConfirmationCodeAsync(It.IsAny<User>(), It.IsAny<string>())) | |
.Callback<User, string>((_, code) => IdentityConfirmationCode = code) | |
.ReturnsAsync(() => new[] { "SMS", "EMAIL " }); | |
MockUserMessagingService | |
.Setup(x => x.SendSignInCodeAsync(It.IsAny<User>(), It.IsAny<string>())) | |
.Callback<User, string>((_, code) => SignInOneTimeCode = code) | |
.ReturnsAsync(() => new[] { "SMS", "EMAIL" }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment