Skip to content

Instantly share code, notes, and snippets.

@mrpmorris
Created July 21, 2022 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrpmorris/7a9378c18f690a504d5507cb0e148bec to your computer and use it in GitHub Desktop.
Save mrpmorris/7a9378c18f690a504d5507cb0e148bec to your computer and use it in GitHub Desktop.
Web integration testing with asp.net and WebSockets
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