From 710b85c935656fbaea085bb3fb5563235c332528 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Fri, 24 May 2024 08:04:45 +0200 Subject: [PATCH] Added silent disposal for the WebApplicationFactory in case there's some dependency (like Marten ProjectCoordinator atm) that sometimes fails during disposal. That should not make tests fail, even though it's not a great thing. Added possibility to inject ILoggerProvider to enable injecting XUnit test output helper. Bumped to 0.8.2 --- src/Ogooreck/API/ApiSpecification.cs | 19 +++++++++++++++++-- src/Ogooreck/Ogooreck.csproj | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Ogooreck/API/ApiSpecification.cs b/src/Ogooreck/API/ApiSpecification.cs index f6439ac..9bb187a 100644 --- a/src/Ogooreck/API/ApiSpecification.cs +++ b/src/Ogooreck/API/ApiSpecification.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; #pragma warning disable CS1591 @@ -26,6 +28,10 @@ protected ApiSpecification(WebApplicationFactory applicationFactory) public static ApiSpecification Setup(WebApplicationFactory applicationFactory) => new(applicationFactory); + public static ApiSpecification With(ILoggerProvider loggerProvider) => + new(new WebApplicationFactory().WithWebHostBuilder( + b => b.ConfigureServices(s => s.AddSingleton(loggerProvider)))); + public GivenApiSpecificationBuilder Given( params RequestDefinition[] builders ) => @@ -70,8 +76,17 @@ public async Task Scenario( return response; } - public void Dispose() => - applicationFactory.Dispose(); + public void Dispose() + { + try + { + applicationFactory.Dispose(); + } + catch (Exception exc) + { + Console.WriteLine($"Error disposing WebApplicationFactory: {exc}"); + } + } } public class TestApiRequest diff --git a/src/Ogooreck/Ogooreck.csproj b/src/Ogooreck/Ogooreck.csproj index 9b56985..e7af7a4 100644 --- a/src/Ogooreck/Ogooreck.csproj +++ b/src/Ogooreck/Ogooreck.csproj @@ -1,7 +1,7 @@ - 0.8.1 + 0.8.2 net6.0;net7.0;net8.0 true true