Created
July 21, 2022 15:37
-
-
Save mrpmorris/7a9378c18f690a504d5507cb0e148bec to your computer and use it in GitHub Desktop.
Web integration testing with asp.net and WebSockets
This file contains hidden or 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
public static class IntegrationTestsServer | |
{ | |
static IntegrationTestsServer() | |
{ | |
ConfigureMocks(); | |
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "IntegrationTesting"); | |
WebApplicationFactory<Program> appBuilder = new WebApplicationFactory<Program>() | |
.WithWebHostBuilder(builder => | |
{ | |
builder.ConfigureTestServices(services => | |
{ | |
services.AddScoped(sp => MockUserMessagingService.Object); | |
services.AddSingleton(sp => MockDealerStrategy.Object); | |
}); | |
builder.UseSetting("https_port", "8080"); | |
}); | |
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(); | |
TestServer = appBuilder.Server; | |
HttpClient = TestServer.CreateClient(); | |
} | |
public static Task<WebSocket> ConnectWebSocketAsync() => | |
TestServer.CreateWebSocketClient().ConnectAsync( | |
new Uri("https://localhost:8080/game-server"), | |
CancellationToken.None); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment