Skip to content

Commit

Permalink
Added silent disposal for the WebApplicationFactory in case there's s…
Browse files Browse the repository at this point in the history
…ome 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
  • Loading branch information
oskardudycz committed May 24, 2024
1 parent b0ba653 commit 710b85c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/Ogooreck/API/ApiSpecification.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

#pragma warning disable CS1591

Expand Down Expand Up @@ -26,6 +28,10 @@ protected ApiSpecification(WebApplicationFactory<TProgram> applicationFactory)
public static ApiSpecification<TProgram> Setup(WebApplicationFactory<TProgram> applicationFactory) =>
new(applicationFactory);

public static ApiSpecification<TProgram> With(ILoggerProvider loggerProvider) =>
new(new WebApplicationFactory<TProgram>().WithWebHostBuilder(
b => b.ConfigureServices(s => s.AddSingleton(loggerProvider))));

public GivenApiSpecificationBuilder Given(
params RequestDefinition[] builders
) =>
Expand Down Expand Up @@ -70,8 +76,17 @@ public async Task<HttpResponseMessage> 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
Expand Down
2 changes: 1 addition & 1 deletion src/Ogooreck/Ogooreck.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>0.8.1</VersionPrefix>
<VersionPrefix>0.8.2</VersionPrefix>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>true</GenerateAssemblyDescriptionAttribute>
Expand Down

0 comments on commit 710b85c

Please sign in to comment.