diff --git a/.docfx/Dockerfile.docfx b/.docfx/Dockerfile.docfx
index 69153cc..aa53eba 100644
--- a/.docfx/Dockerfile.docfx
+++ b/.docfx/Dockerfile.docfx
@@ -1,4 +1,4 @@
-ARG NGINX_VERSION=1.30.0-alpine
+ARG NGINX_VERSION=1.31.0-alpine
FROM --platform=$BUILDPLATFORM nginx:${NGINX_VERSION} AS base
RUN rm -rf /usr/share/nginx/html/*
diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md
index c646bce..2e6351e 100644
--- a/.github/copilot-instructions.md
+++ b/.github/copilot-instructions.md
@@ -17,16 +17,15 @@ This ensures consistent setup, teardown, and output handling across all tests.
using Codebelt.Extensions.Xunit;
using Xunit;
-namespace Your.Namespace
+namespace Your.Namespace;
+
+public class YourTestClass : Test
{
- public class YourTestClass : Test
+ public YourTestClass(ITestOutputHelper output) : base(output)
{
- public YourTestClass(ITestOutputHelper output) : base(output)
- {
- }
-
- // Your tests here
}
+
+ // Your tests here
}
```
@@ -47,21 +46,20 @@ namespace Your.Namespace
## 5. File and Namespace Organization
+- **Always use file-scoped namespaces** (`namespace YourProject.Foo.Bar;`) — the entire codebase has been refactored to file-scoped namespaces. Never use block-scoped namespaces.
- Place test files in the appropriate test project and folder structure.
- Use namespaces that mirror the source code structure. The namespace of a test file MUST match the namespace of the System Under Test (SUT). Do NOT append ".Tests", ".Benchmarks" or similar suffixes to the namespace. Only the assembly/project name should indicate that the file is a test/benchmark (for example: YourProject.Foo.Tests assembly, but namespace YourProject.Foo).
- Example: If the SUT class is declared as:
```csharp
- namespace YourProject.Foo.Bar
- {
- public class Zoo { /* ... */ }
- }
+ namespace YourProject.Foo.Bar;
+
+ public class Zoo { /* ... */ }
```
then the corresponding unit test class must use the exact same namespace:
```csharp
- namespace YourProject.Foo.Bar
- {
- public class ZooTest : Test { /* ... */ }
- }
+ namespace YourProject.Foo.Bar;
+
+ public class ZooTest : Test { /* ... */ }
```
- Do NOT use:
```csharp
@@ -91,16 +89,16 @@ using System.Globalization;
using Codebelt.Extensions.Xunit;
using Xunit;
-namespace YourProject
+namespace YourProject;
+
+///
+/// Tests for the class.
+///
+public class DateSpanTest : Test
{
- ///
- /// Tests for the class.
- ///
- public class DateSpanTest : Test
+ public DateSpanTest(ITestOutputHelper output) : base(output)
{
- public DateSpanTest(ITestOutputHelper output) : base(output)
- {
- }
+ }
[Fact]
public void Parse_ShouldGetOneMonthOfDifference_UsingIso8601String()
@@ -132,7 +130,6 @@ namespace YourProject
TestOutput.WriteLine(span.ToString());
}
- }
}
```
diff --git a/.nuget/Codebelt.Extensions.Xunit.App/PackageReleaseNotes.txt b/.nuget/Codebelt.Extensions.Xunit.App/PackageReleaseNotes.txt
index e550068..cc50285 100644
--- a/.nuget/Codebelt.Extensions.Xunit.App/PackageReleaseNotes.txt
+++ b/.nuget/Codebelt.Extensions.Xunit.App/PackageReleaseNotes.txt
@@ -1,9 +1,15 @@
-Version: 11.0.9
+Version: 11.0.10
Availability: .NET 10 and .NET 9
-
-# ALM
-- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
-
+
+# ALM
+- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
+
+Version: 11.0.9
+Availability: .NET 10 and .NET 9
+
+# ALM
+- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
+
Version: 11.0.8
Availability: .NET 10 and .NET 9
diff --git a/.nuget/Codebelt.Extensions.Xunit.App/README.md b/.nuget/Codebelt.Extensions.Xunit.App/README.md
index 349338c..cb86185 100644
--- a/.nuget/Codebelt.Extensions.Xunit.App/README.md
+++ b/.nuget/Codebelt.Extensions.Xunit.App/README.md
@@ -11,7 +11,7 @@ It is, by heart, free, flexible and built to extend and boost your agile codebel
## **Codebelt.Extensions.Xunit.App** for .NET
-`Codebelt.Extensions.Xunit.App`, aka `Extensions for xUnit API by Codebelt`, is a toolbelt of assemblies designed to extend upon [xUnit.net](https://xunit.net/) for more advanced unit test scenarios while also being the preferred unit test platform of [Cuemon for .NET](https://github.com/gimlichael/Cuemon).
+`Codebelt.Extensions.Xunit.App`, aka `Extensions for xUnit API by Codebelt`, is a toolbelt of assemblies designed to extend upon [xUnit.net](https://xunit.net/) for more advanced unit test scenarios while also being the preferred unit test platform of [Cuemon for .NET](https://github.com/codebeltnet/cuemon).
More documentation available at our documentation site:
diff --git a/.nuget/Codebelt.Extensions.Xunit.Hosting.AspNetCore/PackageReleaseNotes.txt b/.nuget/Codebelt.Extensions.Xunit.Hosting.AspNetCore/PackageReleaseNotes.txt
index d8ca9ad..6f1f329 100644
--- a/.nuget/Codebelt.Extensions.Xunit.Hosting.AspNetCore/PackageReleaseNotes.txt
+++ b/.nuget/Codebelt.Extensions.Xunit.Hosting.AspNetCore/PackageReleaseNotes.txt
@@ -1,9 +1,15 @@
-Version: 11.0.9
+Version: 11.0.10
Availability: .NET 10 and .NET 9
-
-# ALM
-- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
-
+
+# ALM
+- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
+
+Version: 11.0.9
+Availability: .NET 10 and .NET 9
+
+# ALM
+- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
+
Version: 11.0.8
Availability: .NET 10 and .NET 9
diff --git a/.nuget/Codebelt.Extensions.Xunit.Hosting.AspNetCore/README.md b/.nuget/Codebelt.Extensions.Xunit.Hosting.AspNetCore/README.md
index 46ff128..688a810 100644
--- a/.nuget/Codebelt.Extensions.Xunit.Hosting.AspNetCore/README.md
+++ b/.nuget/Codebelt.Extensions.Xunit.Hosting.AspNetCore/README.md
@@ -30,7 +30,7 @@ More documentation available at our documentation site:
### CSharp Example
-Source: [ServerTimingMiddlewareTest.cs](https://github.com/gimlichael/Cuemon/blob/main/test/Cuemon.AspNetCore.Tests/Diagnostics/ServerTimingMiddlewareTest.cs)
+Source: [ServerTimingMiddlewareTest.cs](https://github.com/codebeltnet/cuemon/blob/main/test/Cuemon.AspNetCore.Tests/Diagnostics/ServerTimingMiddlewareTest.cs)
```csharp
[Fact]
diff --git a/.nuget/Codebelt.Extensions.Xunit.Hosting/PackageReleaseNotes.txt b/.nuget/Codebelt.Extensions.Xunit.Hosting/PackageReleaseNotes.txt
index 3819706..0c1dcc2 100644
--- a/.nuget/Codebelt.Extensions.Xunit.Hosting/PackageReleaseNotes.txt
+++ b/.nuget/Codebelt.Extensions.Xunit.Hosting/PackageReleaseNotes.txt
@@ -1,9 +1,15 @@
-Version: 11.0.9
+Version: 11.0.10
Availability: .NET 10, .NET 9 and .NET Standard 2.0
-
-# ALM
-- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
-
+
+# ALM
+- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
+
+Version: 11.0.9
+Availability: .NET 10, .NET 9 and .NET Standard 2.0
+
+# ALM
+- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
+
Version: 11.0.8
Availability: .NET 10, .NET 9 and .NET Standard 2.0
diff --git a/.nuget/Codebelt.Extensions.Xunit.Hosting/README.md b/.nuget/Codebelt.Extensions.Xunit.Hosting/README.md
index 795d2c0..27c0ece 100644
--- a/.nuget/Codebelt.Extensions.Xunit.Hosting/README.md
+++ b/.nuget/Codebelt.Extensions.Xunit.Hosting/README.md
@@ -27,13 +27,13 @@ More documentation available at our documentation site:
### CSharp Example
```csharp
-public class HostTestTest : HostTest
+public class HostTestTest : HostTest
{
private readonly IServiceProvider _provider;
- public HostTestTest(HostFixture hostFixture, ITestOutputHelper output) : base(hostFixture, output)
+ public HostTestTest(ManagedHostFixture hostFixture, ITestOutputHelper output) : base(hostFixture, output)
{
- _provider = hostFixture.ServiceProvider;
+ _provider = hostFixture.Host?.Services;
_provider.GetRequiredService().TestOutput = output;
}
@@ -47,4 +47,4 @@ public class HostTestTest : HostTest
}
```
-A similar but real life example can be found here: [AspNetCoreHostTestTest.cs](https://github.com/codebeltnet/xunit/tree/main/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs)
+A similar but real life example can be found here: [WebHostTestTest.cs](https://github.com/codebeltnet/xunit/blob/main/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/WebHostTestTest.cs)
diff --git a/.nuget/Codebelt.Extensions.Xunit/PackageReleaseNotes.txt b/.nuget/Codebelt.Extensions.Xunit/PackageReleaseNotes.txt
index 28be718..5a4e24c 100644
--- a/.nuget/Codebelt.Extensions.Xunit/PackageReleaseNotes.txt
+++ b/.nuget/Codebelt.Extensions.Xunit/PackageReleaseNotes.txt
@@ -1,9 +1,15 @@
-Version: 11.0.9
+Version: 11.0.10
Availability: .NET 10 and .NET 9
-
-# ALM
-- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
-
+
+# ALM
+- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
+
+Version: 11.0.9
+Availability: .NET 10 and .NET 9
+
+# ALM
+- CHANGED Dependencies have been upgraded to the latest compatible versions for all supported target frameworks (TFMs)
+
Version: 11.0.8
Availability: .NET 10 and .NET 9
diff --git a/AGENTS.md b/AGENTS.md
index 5083095..0faab13 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -91,9 +91,8 @@ dotnet test --collect:"XPlat Code Coverage"
- Keep existing style in files; many modern analyzers are explicitly disabled.
### Namespace Style
-- **Prefer file-scoped namespaces** (`namespace Codebelt.Extensions.Xunit;`) for new files.
-- The current majority of the codebase uses **block-scoped namespaces** — do not convert existing files unless explicitly asked.
-- When editing an existing file, follow whichever style that file already uses.
+- **Always use file-scoped namespaces** (`namespace Codebelt.Extensions.Xunit;`) — the entire codebase has been refactored to file-scoped namespaces.
+- **Never use block-scoped namespaces** for new or edited files.
- **Never use top-level statements.** Always use explicit class declarations with a proper namespace.
### Disabled Analyzers (key rules — do NOT introduce these patterns)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0705687..494ae73 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,27 @@ For more details, please refer to `PackageReleaseNotes.txt` on a per assembly ba
> [!NOTE]
> Changelog entries prior to version 8.4.0 was migrated from previous versions of Cuemon.Extensions.Xunit, Cuemon.Extensions.Xunit.Hosting, and Cuemon.Extensions.Xunit.Hosting.AspNetCore.
+## [11.0.10] - 2026-05-21
+
+This is a patch release focused on codebase modernization, enhanced testing coverage, and developer workflow improvements.
+
+### Changed
+
+- Refactored entire codebase across all three assemblies to use file-scoped namespaces for consistency with modern C# style conventions,
+- Updated developer instructions in `.github/copilot-instructions.md` and `AGENTS.md` to mandate file-scoped namespaces throughout the project,
+- Upgraded NGINX base image in documentation build Dockerfile to version 1.31.0-alpine for improved security and stability,
+- Normalized whitespace and line endings across dependency configuration and package metadata files for consistency.
+
+### Added
+
+- Comprehensive unit tests for ASP.NET Core hosting fixtures including BlockingManagedWebHostFixture, SelfManagedWebHostFixture, SelfManagedWebMinimalHostFixture, HostBuilderApplicationExtensions, and FakeHttpResponseFeature to ensure robust hosting infrastructure,
+- Unit test coverage for HTTP response feature mocking and request/response handling.
+
+### Fixed
+
+- Updated package README examples to reflect current API usage patterns with ManagedHostFixture and correct service provider access,
+- Corrected GitHub repository references in package documentation to point to the codebeltnet organization namespace.
+
## [11.0.9] - 2026-04-16
This is a service update that focuses on package dependencies.
@@ -360,3 +381,15 @@ This major release is first and foremost focused on ironing out any wrinkles tha
- Added null conditional operator to the ServiceProvider property on the HostFixture class in the Codebelt.Extensions.Xunit.Hosting namespace
+
+[Unreleased]: https://github.com/codebeltnet/xunit/compare/v11.0.10...HEAD
+[11.0.10]: https://github.com/codebeltnet/xunit/compare/v11.0.9...v11.0.10
+[11.0.9]: https://github.com/codebeltnet/xunit/compare/v11.0.8...v11.0.9
+[11.0.8]: https://github.com/codebeltnet/xunit/compare/v11.0.7...v11.0.8
+[11.0.7]: https://github.com/codebeltnet/xunit/compare/v11.0.6...v11.0.7
+[11.0.6]: https://github.com/codebeltnet/xunit/compare/v11.0.5...v11.0.6
+[11.0.5]: https://github.com/codebeltnet/xunit/compare/v11.0.4...v11.0.5
+[11.0.4]: https://github.com/codebeltnet/xunit/compare/v11.0.3...v11.0.4
+[11.0.3]: https://github.com/codebeltnet/xunit/compare/v11.0.2...v11.0.3
+[11.0.2]: https://github.com/codebeltnet/xunit/compare/v11.0.1...v11.0.2
+[11.0.1]: https://github.com/codebeltnet/xunit/compare/v11.0.0...v11.0.1
diff --git a/Directory.Packages.props b/Directory.Packages.props
index 148d8e3..4494ab0 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -6,16 +6,16 @@
-
-
-
-
-
+
+
+
+
+
-
-
+
+
@@ -23,23 +23,23 @@
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/BlockingManagedWebHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/BlockingManagedWebHostFixture.cs
index d4b2c1b..303adb4 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/BlockingManagedWebHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/BlockingManagedWebHostFixture.cs
@@ -1,24 +1,23 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Extends the default implementation of the interface to be synchronous e.g., blocking where exceptions can be captured.
+///
+///
+public sealed class BlockingManagedWebHostFixture : ManagedWebHostFixture
{
///
- /// Extends the default implementation of the interface to be synchronous e.g., blocking where exceptions can be captured.
+ /// Initializes a new instance of the class.
///
- ///
- public sealed class BlockingManagedWebHostFixture : ManagedWebHostFixture
+ public BlockingManagedWebHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- public BlockingManagedWebHostFixture()
+ AsyncHostRunnerCallback = (host, _) =>
{
- AsyncHostRunnerCallback = (host, _) =>
- {
- host.Start();
- return Task.CompletedTask;
- };
- }
+ host.Start();
+ return Task.CompletedTask;
+ };
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/GlobalSuppressions.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/GlobalSuppressions.cs
index 72b8b95..97a14ca 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/GlobalSuppressions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/GlobalSuppressions.cs
@@ -1,12 +1,12 @@
-// This file is used by Code Analysis to maintain SuppressMessage
+// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
using System.Diagnostics.CodeAnalysis;
-[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.WebHostTestFactory.Create(System.Action{Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.AspNetCore.Builder.IApplicationBuilder},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},Codebelt.Extensions.Xunit.Hosting.AspNetCore.IAspNetCoreHostFixture)~Codebelt.Extensions.Xunit.Hosting.AspNetCore.IWebHostTest")]
-[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.WebHostTestFactory.CreateWithHostBuilderContext(System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.AspNetCore.Builder.IApplicationBuilder},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},Codebelt.Extensions.Xunit.Hosting.AspNetCore.IAspNetCoreHostFixture)~Codebelt.Extensions.Xunit.Hosting.AspNetCore.IWebHostTest")]
-[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.WebHostTestFactory.RunAsync(System.Action{Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.AspNetCore.Builder.IApplicationBuilder},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},System.Func{System.Net.Http.HttpClient,System.Threading.Tasks.Task{System.Net.Http.HttpResponseMessage}},Codebelt.Extensions.Xunit.Hosting.AspNetCore.IAspNetCoreHostFixture)~System.Threading.Tasks.Task{System.Net.Http.HttpResponseMessage}")]
-[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.WebHostTestFactory.RunWithHostBuilderContextAsync(System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.AspNetCore.Builder.IApplicationBuilder},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},System.Func{System.Net.Http.HttpClient,System.Threading.Tasks.Task{System.Net.Http.HttpResponseMessage}},Codebelt.Extensions.Xunit.Hosting.AspNetCore.IAspNetCoreHostFixture)~System.Threading.Tasks.Task{System.Net.Http.HttpResponseMessage}")]
+[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.WebHostTestFactory.Create(System.Action{Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.AspNetCore.Builder.IApplicationBuilder},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},Codebelt.Extensions.Xunit.Hosting.AspNetCore.IWebHostFixture)~Codebelt.Extensions.Xunit.Hosting.AspNetCore.IWebHostTest")]
+[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.WebHostTestFactory.CreateWithHostBuilderContext(System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.AspNetCore.Builder.IApplicationBuilder},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},Codebelt.Extensions.Xunit.Hosting.AspNetCore.IWebHostFixture)~Codebelt.Extensions.Xunit.Hosting.AspNetCore.IWebHostTest")]
+[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.WebHostTestFactory.RunAsync(System.Action{Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.AspNetCore.Builder.IApplicationBuilder},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},System.Func{System.Net.Http.HttpClient,System.Threading.Tasks.Task{System.Net.Http.HttpResponseMessage}},Codebelt.Extensions.Xunit.Hosting.AspNetCore.IWebHostFixture)~System.Threading.Tasks.Task{System.Net.Http.HttpResponseMessage}")]
+[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.WebHostTestFactory.RunWithHostBuilderContextAsync(System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.AspNetCore.Builder.IApplicationBuilder},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},System.Func{System.Net.Http.HttpClient,System.Threading.Tasks.Task{System.Net.Http.HttpResponseMessage}},Codebelt.Extensions.Xunit.Hosting.AspNetCore.IWebHostFixture)~System.Threading.Tasks.Task{System.Net.Http.HttpResponseMessage}")]
[assembly: SuppressMessage("Performance", "CA1859:Use concrete types when possible for improved performance", Justification = "Nitpick.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http.FakeHttpContextAccessor.MakeGreeting(System.String)~System.IO.Stream")]
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HostBuilderApplicationExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HostBuilderApplicationExtensions.cs
index 338f1ba..a1aa158 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HostBuilderApplicationExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HostBuilderApplicationExtensions.cs
@@ -2,25 +2,24 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Provides extension methods for .
+///
+public static class HostBuilderApplicationExtensions
{
///
- /// Provides extension methods for .
+ /// Converts an to an .
///
- public static class HostBuilderApplicationExtensions
+ /// The to convert.
+ /// The instance.
+ ///
+ /// is not a .
+ ///
+ public static IHostBuilder ToHostBuilder(this IHostApplicationBuilder builder)
{
- ///
- /// Converts an to an .
- ///
- /// The to convert.
- /// The instance.
- ///
- /// is not a .
- ///
- public static IHostBuilder ToHostBuilder(this IHostApplicationBuilder builder)
- {
- if (builder is WebApplicationBuilder webAppBuilder) { return webAppBuilder.Host; }
- throw new ArgumentException($"The provided IHostApplicationBuilder is not a {nameof(WebApplicationBuilder)}.", nameof(builder));
- }
+ if (builder is WebApplicationBuilder webAppBuilder) { return webAppBuilder.Host; }
+ throw new ArgumentException($"The provided IHostApplicationBuilder is not a {nameof(WebApplicationBuilder)}.", nameof(builder));
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/FakeHttpContextAccessor.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/FakeHttpContextAccessor.cs
index a6c7cb1..49fd808 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/FakeHttpContextAccessor.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/FakeHttpContextAccessor.cs
@@ -6,66 +6,65 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http;
+
+///
+/// Provides a unit test implementation of .
+///
+///
+public class FakeHttpContextAccessor : IHttpContextAccessor
{
///
- /// Provides a unit test implementation of .
+ /// Initializes a new instance of the class.
///
- ///
- public class FakeHttpContextAccessor : IHttpContextAccessor
+ /// An optional for resolving services.
+ public FakeHttpContextAccessor(IServiceScopeFactory factory = null)
{
- ///
- /// Initializes a new instance of the class.
- ///
- /// An optional for resolving services.
- public FakeHttpContextAccessor(IServiceScopeFactory factory = null)
- {
- var context = new DefaultHttpContext();
- var fc = new FeatureCollection();
- fc.Set(new RequestServicesFeature(context, factory));
- fc.Set(new FakeHttpResponseFeature());
- fc.Set(new FakeHttpRequestFeature());
- fc.Set(new StreamResponseBodyFeature(MakeGreeting("Hello awesome developers!")));
- context.Uninitialize();
- context.Initialize(fc);
- HttpContext = context;
- }
+ var context = new DefaultHttpContext();
+ var fc = new FeatureCollection();
+ fc.Set(new RequestServicesFeature(context, factory));
+ fc.Set(new FakeHttpResponseFeature());
+ fc.Set(new FakeHttpRequestFeature());
+ fc.Set(new StreamResponseBodyFeature(MakeGreeting("Hello awesome developers!")));
+ context.Uninitialize();
+ context.Initialize(fc);
+ HttpContext = context;
+ }
- private static Stream MakeGreeting(string greeting)
+ private static Stream MakeGreeting(string greeting)
+ {
+ Stream interim = null;
+ Stream result = null;
+ try
{
- Stream interim = null;
- Stream result = null;
- try
- {
- interim = new MemoryStream();
+ interim = new MemoryStream();
- using (var sw = new StreamWriter(interim, Encoding.UTF8, leaveOpen: true))
- {
- sw.Write(greeting);
- sw.Flush();
- }
-
- interim.Flush();
- interim.Position = 0;
-
- result = interim;
- interim = null;
- }
- catch (Exception ex)
- {
- throw new InvalidOperationException("There is an error in the Stream being written.", ex);
- }
- finally
+ using (var sw = new StreamWriter(interim, Encoding.UTF8, leaveOpen: true))
{
- interim?.Dispose();
+ sw.Write(greeting);
+ sw.Flush();
}
- return result;
- }
- ///
- /// Gets or sets the HTTP context.
- ///
- /// The HTTP context.
- public HttpContext HttpContext { get; set; }
+ interim.Flush();
+ interim.Position = 0;
+
+ result = interim;
+ interim = null;
+ }
+ catch (Exception ex)
+ {
+ throw new InvalidOperationException("There is an error in the Stream being written.", ex);
+ }
+ finally
+ {
+ interim?.Dispose();
+ }
+ return result;
}
+
+ ///
+ /// Gets or sets the HTTP context.
+ ///
+ /// The HTTP context.
+ public HttpContext HttpContext { get; set; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/Features/FakeHttpRequestFeature.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/Features/FakeHttpRequestFeature.cs
index 56a0ace..1698a7c 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/Features/FakeHttpRequestFeature.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/Features/FakeHttpRequestFeature.cs
@@ -1,23 +1,22 @@
using System.Net.Http;
using Microsoft.AspNetCore.Http.Features;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http.Features
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http.Features;
+
+///
+/// Represents a way to support some default values for Request context..
+///
+///
+public class FakeHttpRequestFeature : HttpRequestFeature
{
///
- /// Represents a way to support some default values for Request context..
+ /// Initializes a new instance of the class.
///
- ///
- public class FakeHttpRequestFeature : HttpRequestFeature
+ public FakeHttpRequestFeature()
{
- ///
- /// Initializes a new instance of the class.
- ///
- public FakeHttpRequestFeature()
- {
- Method = HttpMethod.Get.ToString();
- Path = "/";
- Scheme = "http";
- Protocol = "HTTP/1.1";
- }
+ Method = HttpMethod.Get.ToString();
+ Path = "/";
+ Scheme = "http";
+ Protocol = "HTTP/1.1";
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/Features/FakeHttpResponseFeature.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/Features/FakeHttpResponseFeature.cs
index 721e8b1..e0469ce 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/Features/FakeHttpResponseFeature.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Http/Features/FakeHttpResponseFeature.cs
@@ -4,42 +4,41 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Net.Http.Headers;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http.Features
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http.Features;
+
+///
+/// Represents a way to trigger .
+///
+///
+public class FakeHttpResponseFeature : HttpResponseFeature
{
+ private bool _hasStarted;
+
///
- /// Represents a way to trigger .
+ /// Initializes a new instance of the class.
///
- ///
- public class FakeHttpResponseFeature : HttpResponseFeature
+ public FakeHttpResponseFeature()
{
- private bool _hasStarted;
-
- ///
- /// Initializes a new instance of the class.
- ///
- public FakeHttpResponseFeature()
- {
- Headers.Append(HeaderNames.Date, DateTime.UtcNow.ToString("R"));
- }
-
- ///
- /// Registers a callback to be invoked just before the response starts. This is the last chance to modify the , , or .
- ///
- /// The callback to invoke when starting the response.
- /// The state to pass into the callback.
- public override void OnStarting(Func callback, object state)
- {
- if (_hasStarted) { return; }
- _hasStarted = true;
- callback?.Invoke(state);
- }
+ Headers.Append(HeaderNames.Date, DateTime.UtcNow.ToString("R"));
+ }
- ///
- /// Indicates if the response has started. If true, the ,
- /// , and are now immutable, and
- /// OnStarting should no longer be called.
- ///
- /// true if this instance has started; otherwise, false .
- public override bool HasStarted => _hasStarted;
+ ///
+ /// Registers a callback to be invoked just before the response starts. This is the last chance to modify the , , or .
+ ///
+ /// The callback to invoke when starting the response.
+ /// The state to pass into the callback.
+ public override void OnStarting(Func callback, object state)
+ {
+ if (_hasStarted) { return; }
+ _hasStarted = true;
+ callback?.Invoke(state);
}
+
+ ///
+ /// Indicates if the response has started. If true, the ,
+ /// , and are now immutable, and
+ /// OnStarting should no longer be called.
+ ///
+ /// true if this instance has started; otherwise, false .
+ public override bool HasStarted => _hasStarted;
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HttpClientExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HttpClientExtensions.cs
index 888561c..74cb697 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HttpClientExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HttpClientExtensions.cs
@@ -2,28 +2,27 @@
using System.Net.Http;
using System.Threading.Tasks;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Provides extension methods for the class.
+///
+public static class HttpClientExtensions
{
///
- /// Provides extension methods for the class.
+ /// Provides a convenient way to return a from a using the specified .
///
- public static class HttpClientExtensions
+ /// The to extend.
+ /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
+ /// A that represents the asynchronous operation. The task result contains the generated by the .
+ ///
+ /// cannot be null.
+ ///
+ /// Designed to be used in conjunction with and .
+ public static async Task ToHttpResponseMessageAsync(this HttpClient client, Func> responseFactory = null)
{
- ///
- /// Provides a convenient way to return a from a using the specified .
- ///
- /// The to extend.
- /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
- /// A that represents the asynchronous operation. The task result contains the generated by the .
- ///
- /// cannot be null.
- ///
- /// Designed to be used in conjunction with and .
- public static async Task ToHttpResponseMessageAsync(this HttpClient client, Func> responseFactory = null)
- {
- ArgumentNullException.ThrowIfNull(client);
- responseFactory ??= c => c.GetAsync("/");
- return await responseFactory(client).ConfigureAwait(false);
- }
+ ArgumentNullException.ThrowIfNull(client);
+ responseFactory ??= c => c.GetAsync("/");
+ return await responseFactory(client).ConfigureAwait(false);
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IPipelineTest.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IPipelineTest.cs
index d051cf4..163dcb7 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IPipelineTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IPipelineTest.cs
@@ -1,17 +1,16 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Represents the members needed for ASP.NET Core pipeline testing.
+///
+public interface IPipelineTest
{
///
- /// Represents the members needed for ASP.NET Core pipeline testing.
+ /// Gets the initialized by the .
///
- public interface IPipelineTest
- {
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- IApplicationBuilder Application { get; }
- }
+ /// The initialized by the .
+ IApplicationBuilder Application { get; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebHostFixture.cs
index 23abfde..fa0131a 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebHostFixture.cs
@@ -1,18 +1,17 @@
using System;
using Microsoft.AspNetCore.Builder;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Provides a way to use Microsoft Dependency Injection in unit tests tailored for ASP.NET Core.
+///
+///
+public interface IWebHostFixture : IGenericHostFixture, IPipelineTest
{
///
- /// Provides a way to use Microsoft Dependency Injection in unit tests tailored for ASP.NET Core.
+ /// Gets or sets the delegate that configures the HTTP request pipeline.
///
- ///
- public interface IWebHostFixture : IGenericHostFixture, IPipelineTest
- {
- ///
- /// Gets or sets the delegate that configures the HTTP request pipeline.
- ///
- /// The delegate that configures the HTTP request pipeline.
- Action ConfigureApplicationCallback { get; set; }
- }
+ /// The delegate that configures the HTTP request pipeline.
+ Action ConfigureApplicationCallback { get; set; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebHostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebHostTest.cs
index eb540e6..fca9af9 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebHostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebHostTest.cs
@@ -1,11 +1,10 @@
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Represents the members needed for ASP.NET Core (including but not limited to MVC, Razor and related) testing.
+///
+///
+///
+public interface IWebHostTest : IHostTest, IPipelineTest
{
- ///
- /// Represents the members needed for ASP.NET Core (including but not limited to MVC, Razor and related) testing.
- ///
- ///
- ///
- public interface IWebHostTest : IHostTest, IPipelineTest
- {
- }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebMinimalHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebMinimalHostFixture.cs
index 1845af0..23ae0b5 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebMinimalHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IWebMinimalHostFixture.cs
@@ -1,19 +1,18 @@
using System;
using Microsoft.AspNetCore.Builder;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Provides a way to use Microsoft Dependency Injection in unit tests (minimal style).
+///
+///
+///
+public interface IWebMinimalHostFixture : IMinimalHostFixture, IPipelineTest
{
///
- /// Provides a way to use Microsoft Dependency Injection in unit tests (minimal style).
+ /// Gets or sets the delegate that configures the HTTP request pipeline.
///
- ///
- ///
- public interface IWebMinimalHostFixture : IMinimalHostFixture, IPipelineTest
- {
- ///
- /// Gets or sets the delegate that configures the HTTP request pipeline.
- ///
- /// The delegate that configures the HTTP request pipeline.
- Action ConfigureApplicationCallback { get; set; }
- }
+ /// The delegate that configures the HTTP request pipeline.
+ Action ConfigureApplicationCallback { get; set; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/MinimalWebHostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/MinimalWebHostTest.cs
index 3c1dbbe..f8287cf 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/MinimalWebHostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/MinimalWebHostTest.cs
@@ -3,70 +3,69 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Internal
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Internal;
+
+internal sealed class MinimalWebHostTest : MinimalWebHostTest
{
- internal sealed class MinimalWebHostTest : MinimalWebHostTest
- {
- private readonly Action _pipelineConfigurator;
- private readonly Action _serviceConfigurator;
- private readonly Action _pipelineConfiguratorWithContext;
- private readonly Action _serviceConfiguratorWithContext;
- private readonly Action _hostConfigurator;
- private HostBuilderContext _hostBuilderContext;
+ private readonly Action _pipelineConfigurator;
+ private readonly Action _serviceConfigurator;
+ private readonly Action _pipelineConfiguratorWithContext;
+ private readonly Action _serviceConfiguratorWithContext;
+ private readonly Action _hostConfigurator;
+ private HostBuilderContext _hostBuilderContext;
- internal MinimalWebHostTest(Action serviceConfigurator, Action pipelineConfigurator, Action hostConfigurator, IWebMinimalHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
- {
- _serviceConfigurator = serviceConfigurator;
- _pipelineConfigurator = pipelineConfigurator;
- _hostConfigurator = hostConfigurator;
- InitializeHostFixture(hostFixture);
- }
+ internal MinimalWebHostTest(Action serviceConfigurator, Action pipelineConfigurator, Action hostConfigurator, IWebMinimalHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
+ {
+ _serviceConfigurator = serviceConfigurator;
+ _pipelineConfigurator = pipelineConfigurator;
+ _hostConfigurator = hostConfigurator;
+ InitializeHostFixture(hostFixture);
+ }
- internal MinimalWebHostTest(Action serviceConfigurator, Action pipelineConfigurator, Action hostConfigurator, IWebMinimalHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
- {
- _serviceConfiguratorWithContext = serviceConfigurator;
- _pipelineConfiguratorWithContext = pipelineConfigurator;
- _hostConfigurator = hostConfigurator;
- InitializeHostFixture(hostFixture);
- }
+ internal MinimalWebHostTest(Action serviceConfigurator, Action pipelineConfigurator, Action hostConfigurator, IWebMinimalHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
+ {
+ _serviceConfiguratorWithContext = serviceConfigurator;
+ _pipelineConfiguratorWithContext = pipelineConfigurator;
+ _hostConfigurator = hostConfigurator;
+ InitializeHostFixture(hostFixture);
+ }
- private void InitializeHostFixture(IWebMinimalHostFixture hostFixture)
+ private void InitializeHostFixture(IWebMinimalHostFixture hostFixture)
+ {
+ if (!hostFixture.HasValidState())
{
- if (!hostFixture.HasValidState())
- {
- hostFixture.ConfigureHostCallback = ConfigureHost;
- hostFixture.ConfigureCallback = Configure;
- hostFixture.ConfigureApplicationCallback = ConfigureApplication;
- hostFixture.ConfigureHost(this);
- }
- Host = hostFixture.Host;
- Application = hostFixture.Application;
- Configure(hostFixture.Configuration, hostFixture.Environment);
+ hostFixture.ConfigureHostCallback = ConfigureHost;
+ hostFixture.ConfigureCallback = Configure;
+ hostFixture.ConfigureApplicationCallback = ConfigureApplication;
+ hostFixture.ConfigureHost(this);
}
+ Host = hostFixture.Host;
+ Application = hostFixture.Application;
+ Configure(hostFixture.Configuration, hostFixture.Environment);
+ }
- public override void ConfigureApplication(IApplicationBuilder app)
+ public override void ConfigureApplication(IApplicationBuilder app)
+ {
+ _pipelineConfigurator?.Invoke(app);
+ _pipelineConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
{
- _pipelineConfigurator?.Invoke(app);
- _pipelineConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
- {
- hbc.Configuration = Configuration;
- hbc.HostingEnvironment = Environment;
- return hbc;
- }), app);
- }
+ hbc.Configuration = Configuration;
+ hbc.HostingEnvironment = Environment;
+ return hbc;
+ }), app);
+ }
- protected override void ConfigureHost(IHostApplicationBuilder hb)
+ protected override void ConfigureHost(IHostApplicationBuilder hb)
+ {
+ _hostBuilderContext = new HostBuilderContext(hb.Properties);
+ _hostConfigurator?.Invoke(hb);
+ _serviceConfigurator?.Invoke(hb.Services);
+ _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
{
- _hostBuilderContext = new HostBuilderContext(hb.Properties);
- _hostConfigurator?.Invoke(hb);
- _serviceConfigurator?.Invoke(hb.Services);
- _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
- {
- hbc.Configuration = hb.Configuration;
- hbc.HostingEnvironment = hb.Environment;
- return hbc;
- }), hb.Services);
- hb.Services.AddFakeHttpContextAccessor();
- }
+ hbc.Configuration = hb.Configuration;
+ hbc.HostingEnvironment = hb.Environment;
+ return hbc;
+ }), hb.Services);
+ hb.Services.AddFakeHttpContextAccessor();
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/WebHostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/WebHostTest.cs
index 8494f1b..7b56f5f 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/WebHostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/WebHostTest.cs
@@ -3,75 +3,74 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Internal
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Internal;
+
+internal sealed class WebHostTest : WebHostTest
{
- internal sealed class WebHostTest : WebHostTest
- {
- private readonly Action _pipelineConfigurator;
- private readonly Action _serviceConfigurator;
- private readonly Action _pipelineConfiguratorWithContext;
- private readonly Action _serviceConfiguratorWithContext;
- private readonly Action _hostConfigurator;
- private HostBuilderContext _hostBuilderContext;
+ private readonly Action _pipelineConfigurator;
+ private readonly Action _serviceConfigurator;
+ private readonly Action _pipelineConfiguratorWithContext;
+ private readonly Action _serviceConfiguratorWithContext;
+ private readonly Action _hostConfigurator;
+ private HostBuilderContext _hostBuilderContext;
- internal WebHostTest(Action serviceConfigurator, Action pipelineConfigurator, Action hostConfigurator, IWebHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
- {
- _serviceConfigurator = serviceConfigurator;
- _pipelineConfigurator = pipelineConfigurator;
- _hostConfigurator = hostConfigurator;
- InitializeHostFixture(hostFixture);
- }
+ internal WebHostTest(Action serviceConfigurator, Action pipelineConfigurator, Action hostConfigurator, IWebHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
+ {
+ _serviceConfigurator = serviceConfigurator;
+ _pipelineConfigurator = pipelineConfigurator;
+ _hostConfigurator = hostConfigurator;
+ InitializeHostFixture(hostFixture);
+ }
- internal WebHostTest(Action serviceConfigurator, Action pipelineConfigurator, Action hostConfigurator, IWebHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
- {
- _serviceConfiguratorWithContext = serviceConfigurator;
- _pipelineConfiguratorWithContext = pipelineConfigurator;
- _hostConfigurator = hostConfigurator;
- InitializeHostFixture(hostFixture);
- }
+ internal WebHostTest(Action serviceConfigurator, Action pipelineConfigurator, Action hostConfigurator, IWebHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
+ {
+ _serviceConfiguratorWithContext = serviceConfigurator;
+ _pipelineConfiguratorWithContext = pipelineConfigurator;
+ _hostConfigurator = hostConfigurator;
+ InitializeHostFixture(hostFixture);
+ }
- private void InitializeHostFixture(IWebHostFixture hostFixture)
+ private void InitializeHostFixture(IWebHostFixture hostFixture)
+ {
+ if (!hostFixture.HasValidState())
{
- if (!hostFixture.HasValidState())
- {
- hostFixture.ConfigureHostCallback = ConfigureHost;
- hostFixture.ConfigureCallback = Configure;
- hostFixture.ConfigureServicesCallback = ConfigureServices;
- hostFixture.ConfigureApplicationCallback = ConfigureApplication;
- hostFixture.ConfigureHost(this);
- }
- Host = hostFixture.Host;
- Application = hostFixture.Application;
- Configure(hostFixture.Configuration, hostFixture.Environment);
+ hostFixture.ConfigureHostCallback = ConfigureHost;
+ hostFixture.ConfigureCallback = Configure;
+ hostFixture.ConfigureServicesCallback = ConfigureServices;
+ hostFixture.ConfigureApplicationCallback = ConfigureApplication;
+ hostFixture.ConfigureHost(this);
}
+ Host = hostFixture.Host;
+ Application = hostFixture.Application;
+ Configure(hostFixture.Configuration, hostFixture.Environment);
+ }
- public override void ConfigureApplication(IApplicationBuilder app)
+ public override void ConfigureApplication(IApplicationBuilder app)
+ {
+ _pipelineConfigurator?.Invoke(app);
+ _pipelineConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
{
- _pipelineConfigurator?.Invoke(app);
- _pipelineConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
- {
- hbc.Configuration = Configuration;
- hbc.HostingEnvironment = Environment;
- return hbc;
- }), app);
- }
+ hbc.Configuration = Configuration;
+ hbc.HostingEnvironment = Environment;
+ return hbc;
+ }), app);
+ }
- protected override void ConfigureHost(IHostBuilder hb)
- {
- _hostBuilderContext = new HostBuilderContext(hb.Properties);
- _hostConfigurator?.Invoke(hb);
- }
+ protected override void ConfigureHost(IHostBuilder hb)
+ {
+ _hostBuilderContext = new HostBuilderContext(hb.Properties);
+ _hostConfigurator?.Invoke(hb);
+ }
- public override void ConfigureServices(IServiceCollection services)
+ public override void ConfigureServices(IServiceCollection services)
+ {
+ _serviceConfigurator?.Invoke(services);
+ _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
{
- _serviceConfigurator?.Invoke(services);
- _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
- {
- hbc.Configuration = Configuration;
- hbc.HostingEnvironment = Environment;
- return hbc;
- }), services);
- services.AddFakeHttpContextAccessor();
- }
+ hbc.Configuration = Configuration;
+ hbc.HostingEnvironment = Environment;
+ return hbc;
+ }), services);
+ services.AddFakeHttpContextAccessor();
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ManagedWebHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ManagedWebHostFixture.cs
index af5ce7c..c7c8332 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ManagedWebHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ManagedWebHostFixture.cs
@@ -9,103 +9,102 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Provides a default implementation of the interface.
+///
+///
+///
+public class ManagedWebHostFixture : ManagedHostFixture, IWebHostFixture
{
///
- /// Provides a default implementation of the interface.
+ /// Initializes a new instance of the class.
///
- ///
- ///
- public class ManagedWebHostFixture : ManagedHostFixture, IWebHostFixture
+ public ManagedWebHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- public ManagedWebHostFixture()
- {
- }
+ }
- ///
- /// Creates and configures the of this instance.
- ///
- /// The object that inherits from .
- /// was added to support those cases where the caller is required in the host configuration.
- ///
- /// is null.
- ///
- ///
- /// is not assignable from .
- ///
- public override void ConfigureHost(Test hostTest)
- {
- ArgumentNullException.ThrowIfNull(hostTest);
- if (!HasTypes(hostTest.GetType(), typeof(WebHostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(WebHostTest<>), $"{nameof(hostTest)} is not assignable from WebHostTest."); }
- if (!this.HasValidState()) { return; } // had to include this due to dual-call this method (one uncontrolled from xUnit library reflection magic; second controlled from this library)
+ ///
+ /// Creates and configures the of this instance.
+ ///
+ /// The object that inherits from .
+ /// was added to support those cases where the caller is required in the host configuration.
+ ///
+ /// is null.
+ ///
+ ///
+ /// is not assignable from .
+ ///
+ public override void ConfigureHost(Test hostTest)
+ {
+ ArgumentNullException.ThrowIfNull(hostTest);
+ if (!HasTypes(hostTest.GetType(), typeof(WebHostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(WebHostTest<>), $"{nameof(hostTest)} is not assignable from WebHostTest."); }
+ if (!this.HasValidState()) { return; } // had to include this due to dual-call this method (one uncontrolled from xUnit library reflection magic; second controlled from this library)
- var hb = new HostBuilder()
- .ConfigureWebHost(webBuilder =>
- {
- webBuilder
- .UseTestServer(o => o.PreserveExecutionContext = true)
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseEnvironment("Development")
- .ConfigureAppConfiguration((context, config) =>
- {
- config.AddJsonFile("appsettings.json", true, true)
- .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true, true)
- .AddEnvironmentVariables();
+ var hb = new HostBuilder()
+ .ConfigureWebHost(webBuilder =>
+ {
+ webBuilder
+ .UseTestServer(o => o.PreserveExecutionContext = true)
+ .UseContentRoot(Directory.GetCurrentDirectory())
+ .UseEnvironment("Development")
+ .ConfigureAppConfiguration((context, config) =>
+ {
+ config.AddJsonFile("appsettings.json", true, true)
+ .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true, true)
+ .AddEnvironmentVariables();
- StaticWebAssetsLoader.UseStaticWebAssets(context.HostingEnvironment, context.Configuration);
+ StaticWebAssetsLoader.UseStaticWebAssets(context.HostingEnvironment, context.Configuration);
- ConfigureCallback(config.Build(), context.HostingEnvironment);
- })
- .ConfigureLogging((context, logging) =>
- {
- logging.AddConfiguration(context.Configuration.GetSection("Logging"));
- logging.AddConsole();
- logging.AddDebug();
- logging.AddEventSourceLogger();
- })
- .ConfigureServices((context, services) =>
+ ConfigureCallback(config.Build(), context.HostingEnvironment);
+ })
+ .ConfigureLogging((context, logging) =>
+ {
+ logging.AddConfiguration(context.Configuration.GetSection("Logging"));
+ logging.AddConsole();
+ logging.AddDebug();
+ logging.AddEventSourceLogger();
+ })
+ .ConfigureServices((context, services) =>
+ {
+ Configuration = context.Configuration;
+ Environment = context.HostingEnvironment;
+ ConfigureServicesCallback(services);
+ })
+ .Configure(app =>
{
- Configuration = context.Configuration;
- Environment = context.HostingEnvironment;
- ConfigureServicesCallback(services);
- })
- .Configure(app =>
- {
- ConfigureApplicationCallback.Invoke(app);
- Application = app;
- }
- )
- .UseSetting(HostDefaults.ApplicationKey, hostTest.CallerType.Assembly.GetName().Name);
- });
+ ConfigureApplicationCallback.Invoke(app);
+ Application = app;
+ }
+ )
+ .UseSetting(HostDefaults.ApplicationKey, hostTest.CallerType.Assembly.GetName().Name);
+ });
#if NET9_0_OR_GREATER
- hb.UseDefaultServiceProvider(o =>
- {
- o.ValidateOnBuild = true;
- o.ValidateScopes = true;
- });
+ hb.UseDefaultServiceProvider(o =>
+ {
+ o.ValidateOnBuild = true;
+ o.ValidateScopes = true;
+ });
#endif
- ConfigureHostCallback(hb);
+ ConfigureHostCallback(hb);
- Host = hb.Build();
+ Host = hb.Build();
- AsyncHostRunnerCallback(Host, CancellationToken.None);
- }
+ AsyncHostRunnerCallback(Host, CancellationToken.None);
+ }
- ///
- /// Gets or sets the delegate that configures the HTTP request pipeline.
- ///
- /// The delegate that configures the HTTP request pipeline.
- public Action ConfigureApplicationCallback { get; set; }
+ ///
+ /// Gets or sets the delegate that configures the HTTP request pipeline.
+ ///
+ /// The delegate that configures the HTTP request pipeline.
+ public Action ConfigureApplicationCallback { get; set; }
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- public IApplicationBuilder Application { get; protected set; }
- }
+ ///
+ /// Gets the initialized by the .
+ ///
+ /// The initialized by the .
+ public IApplicationBuilder Application { get; protected set; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ManagedWebMinimalHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ManagedWebMinimalHostFixture.cs
index 2f1b1e7..29c14f8 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ManagedWebMinimalHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ManagedWebMinimalHostFixture.cs
@@ -4,74 +4,73 @@
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Provides a default implementation of the interface.
+///
+///
+///
+/// This is the "modern" minimal style implementation of .
+public class ManagedWebMinimalHostFixture : ManagedMinimalHostFixture, IWebMinimalHostFixture
{
///
- /// Provides a default implementation of the interface.
+ /// Initializes a new instance of the class.
///
- ///
- ///
- /// This is the "modern" minimal style implementation of .
- public class ManagedWebMinimalHostFixture : ManagedMinimalHostFixture, IWebMinimalHostFixture
+ public ManagedWebMinimalHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- public ManagedWebMinimalHostFixture()
- {
- }
+ }
- ///
- /// Creates and configures the of this instance.
- ///
- /// The object that inherits from .
- /// was added to support those cases where the caller is required in the host configuration.
- ///
- /// is null.
- ///
- ///
- /// is not assignable from .
- ///
- public override void ConfigureHost(Test hostTest)
- {
- ArgumentNullException.ThrowIfNull(hostTest);
- if (!HasTypes(hostTest.GetType(), typeof(MinimalWebHostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(MinimalWebHostTest<>), $"{nameof(hostTest)} is not assignable from MinimalWebHostTest."); }
+ ///
+ /// Creates and configures the of this instance.
+ ///
+ /// The object that inherits from .
+ /// was added to support those cases where the caller is required in the host configuration.
+ ///
+ /// is null.
+ ///
+ ///
+ /// is not assignable from .
+ ///
+ public override void ConfigureHost(Test hostTest)
+ {
+ ArgumentNullException.ThrowIfNull(hostTest);
+ if (!HasTypes(hostTest.GetType(), typeof(MinimalWebHostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(MinimalWebHostTest<>), $"{nameof(hostTest)} is not assignable from MinimalWebHostTest."); }
- var hb = WebApplication.CreateBuilder(new WebApplicationOptions()
- {
- EnvironmentName = "Development",
- ApplicationName = hostTest.CallerType.Assembly.GetName().Name
- });
+ var hb = WebApplication.CreateBuilder(new WebApplicationOptions()
+ {
+ EnvironmentName = "Development",
+ ApplicationName = hostTest.CallerType.Assembly.GetName().Name
+ });
- hb.WebHost.UseTestServer(o => o.PreserveExecutionContext = true);
+ hb.WebHost.UseTestServer(o => o.PreserveExecutionContext = true);
- Configuration = hb.Configuration;
- Environment = hb.Environment;
+ Configuration = hb.Configuration;
+ Environment = hb.Environment;
- ConfigureCallback(Configuration, Environment);
+ ConfigureCallback(Configuration, Environment);
- ConfigureHostCallback(hb);
+ ConfigureHostCallback(hb);
- var webApplication = hb.Build();
+ var webApplication = hb.Build();
- ConfigureApplicationCallback(webApplication);
- Application = webApplication;
-
- Host = webApplication;
+ ConfigureApplicationCallback(webApplication);
+ Application = webApplication;
+
+ Host = webApplication;
- AsyncHostRunnerCallback(Host, CancellationToken.None);
- }
+ AsyncHostRunnerCallback(Host, CancellationToken.None);
+ }
- ///
- /// Gets or sets the delegate that configures the HTTP request pipeline.
- ///
- /// The delegate that configures the HTTP request pipeline.
- public Action ConfigureApplicationCallback { get; set; }
+ ///
+ /// Gets or sets the delegate that configures the HTTP request pipeline.
+ ///
+ /// The delegate that configures the HTTP request pipeline.
+ public Action ConfigureApplicationCallback { get; set; }
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- public IApplicationBuilder Application { get; protected set; }
- }
+ ///
+ /// Gets the initialized by the .
+ ///
+ /// The initialized by the .
+ public IApplicationBuilder Application { get; protected set; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostTest.cs
index 49474c7..c08b789 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostTest.cs
@@ -3,64 +3,63 @@
using Microsoft.Extensions.Hosting;
using Xunit;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection and depends on ASP.NET Core (minimal style), should derive.
+///
+/// The type of the object that implements the interface.
+///
+///
+///
+/// The class needed to be designed in this rather complex way, as this is the only way that xUnit supports a shared context. The need for shared context is theoretical at best, but it does opt-in for Scoped instances.
+public abstract class MinimalWebHostTest : MinimalHostTest, IWebHostTest, IClassFixture where T : class, IWebMinimalHostFixture
{
///
- /// Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection and depends on ASP.NET Core (minimal style), should derive.
+ /// Initializes a new instance of the class.
///
- /// The type of the object that implements the interface.
- ///
- ///
- ///
- /// The class needed to be designed in this rather complex way, as this is the only way that xUnit supports a shared context. The need for shared context is theoretical at best, but it does opt-in for Scoped instances.
- public abstract class MinimalWebHostTest : MinimalHostTest, IWebHostTest, IClassFixture where T : class, IWebMinimalHostFixture
+ /// An implementation of the interface.
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ protected MinimalWebHostTest(T hostFixture, ITestOutputHelper output = null, Type callerType = null) : this(false, hostFixture, output, callerType)
{
- ///
- /// Initializes a new instance of the class.
- ///
- /// An implementation of the interface.
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- protected MinimalWebHostTest(T hostFixture, ITestOutputHelper output = null, Type callerType = null) : this(false, hostFixture, output, callerType)
- {
- }
+ }
- ///
- /// Initializes a new instance of the class.
- ///
- /// A value indicating whether to skip the host fixture initialization.
- /// An implementation of the interface.
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- ///
- /// is null.
- ///
- protected MinimalWebHostTest(bool skipHostFixtureInitialization, T hostFixture, ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// A value indicating whether to skip the host fixture initialization.
+ /// An implementation of the interface.
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ ///
+ /// is null.
+ ///
+ protected MinimalWebHostTest(bool skipHostFixtureInitialization, T hostFixture, ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
+ {
+ ArgumentNullException.ThrowIfNull(hostFixture);
+ if (skipHostFixtureInitialization) { return; }
+ if (!hostFixture.HasValidState())
{
- ArgumentNullException.ThrowIfNull(hostFixture);
- if (skipHostFixtureInitialization) { return; }
- if (!hostFixture.HasValidState())
- {
- hostFixture.ConfigureCallback = Configure;
- hostFixture.ConfigureHostCallback = ConfigureHost;
- hostFixture.ConfigureApplicationCallback = ConfigureApplication;
- hostFixture.ConfigureHost(this);
- }
- Host = hostFixture.Host;
- Application = hostFixture.Application;
- Configure(hostFixture.Configuration, hostFixture.Environment);
+ hostFixture.ConfigureCallback = Configure;
+ hostFixture.ConfigureHostCallback = ConfigureHost;
+ hostFixture.ConfigureApplicationCallback = ConfigureApplication;
+ hostFixture.ConfigureHost(this);
}
+ Host = hostFixture.Host;
+ Application = hostFixture.Application;
+ Configure(hostFixture.Configuration, hostFixture.Environment);
+ }
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- public IApplicationBuilder Application { get; protected set; }
+ ///
+ /// Gets the initialized by the .
+ ///
+ /// The initialized by the .
+ public IApplicationBuilder Application { get; protected set; }
- ///
- /// Configures the HTTP request pipeline.
- ///
- /// The type that provides the mechanisms to configure the HTTP request pipeline.
- public abstract void ConfigureApplication(IApplicationBuilder app);
- }
+ ///
+ /// Configures the HTTP request pipeline.
+ ///
+ /// The type that provides the mechanisms to configure the HTTP request pipeline.
+ public abstract void ConfigureApplication(IApplicationBuilder app);
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostTestFactory.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostTestFactory.cs
index 05780f8..ee843b1 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostTestFactory.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostTestFactory.cs
@@ -7,68 +7,67 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Provides a set of static methods for ASP.NET Core (including, but not limited to MVC, Razor and related) unit testing (minimal style).
+///
+/// .
+public static class MinimalWebHostTestFactory
{
///
- /// Provides a set of static methods for ASP.NET Core (including, but not limited to MVC, Razor and related) unit testing (minimal style).
+ /// Creates and returns an implementation.
///
- /// .
- public static class MinimalWebHostTestFactory
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The which may be configured.
+ /// An optional implementation to use instead of the default instance.
+ /// An instance of an implementation.
+ public static IWebHostTest Create(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, IWebMinimalHostFixture hostFixture = null)
{
- ///
- /// Creates and returns an implementation.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// The which may be configured.
- /// An optional implementation to use instead of the default instance.
- /// An instance of an implementation.
- public static IWebHostTest Create(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, IWebMinimalHostFixture hostFixture = null)
- {
- return new MinimalWebHostTest(serviceSetup, pipelineSetup, hostSetup, hostFixture ?? new ManagedWebMinimalHostFixture());
- }
+ return new MinimalWebHostTest(serviceSetup, pipelineSetup, hostSetup, hostFixture ?? new ManagedWebMinimalHostFixture());
+ }
- ///
- /// Creates and returns an implementation.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// The which may be configured.
- /// An optional implementation to use instead of the default instance.
- /// An instance of an implementation.
- public static IWebHostTest CreateWithHostBuilderContext(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, IWebMinimalHostFixture hostFixture = null)
- {
- return new MinimalWebHostTest(serviceSetup, pipelineSetup, hostSetup, hostFixture ?? new ManagedWebMinimalHostFixture());
- }
+ ///
+ /// Creates and returns an implementation.
+ ///
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The which may be configured.
+ /// An optional implementation to use instead of the default instance.
+ /// An instance of an implementation.
+ public static IWebHostTest CreateWithHostBuilderContext(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, IWebMinimalHostFixture hostFixture = null)
+ {
+ return new MinimalWebHostTest(serviceSetup, pipelineSetup, hostSetup, hostFixture ?? new ManagedWebMinimalHostFixture());
+ }
- ///
- /// Runs a middleware and returns an for making HTTP requests to the test server.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// The which may be configured.
- /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
- /// An optional implementation to use instead of the default instance.
- /// A that represents the asynchronous operation. The task result contains the for the test server.
- public static async Task RunAsync(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, Func> responseFactory = null, IWebMinimalHostFixture hostFixture = null)
- {
- using var client = Create(serviceSetup, pipelineSetup, hostSetup, hostFixture).Host.GetTestClient();
- return await client.ToHttpResponseMessageAsync(responseFactory).ConfigureAwait(false);
- }
+ ///
+ /// Runs a middleware and returns an for making HTTP requests to the test server.
+ ///
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
+ /// An optional implementation to use instead of the default instance.
+ /// A that represents the asynchronous operation. The task result contains the for the test server.
+ public static async Task RunAsync(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, Func> responseFactory = null, IWebMinimalHostFixture hostFixture = null)
+ {
+ using var client = Create(serviceSetup, pipelineSetup, hostSetup, hostFixture).Host.GetTestClient();
+ return await client.ToHttpResponseMessageAsync(responseFactory).ConfigureAwait(false);
+ }
- ///
- /// Runs a filter/middleware test.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// The which may be configured.
- /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
- /// An optional implementation to use instead of the default instance.
- /// A that represents the asynchronous operation. The task result contains the for the test server.
- public static async Task RunWithHostBuilderContextAsync(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, Func> responseFactory = null, IWebMinimalHostFixture hostFixture = null)
- {
- using var client = CreateWithHostBuilderContext(serviceSetup, pipelineSetup, hostSetup, hostFixture).Host.GetTestClient();
- return await client.ToHttpResponseMessageAsync(responseFactory).ConfigureAwait(false);
- }
+ ///
+ /// Runs a filter/middleware test.
+ ///
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
+ /// An optional implementation to use instead of the default instance.
+ /// A that represents the asynchronous operation. The task result contains the for the test server.
+ public static async Task RunWithHostBuilderContextAsync(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, Func> responseFactory = null, IWebMinimalHostFixture hostFixture = null)
+ {
+ using var client = CreateWithHostBuilderContext(serviceSetup, pipelineSetup, hostSetup, hostFixture).Host.GetTestClient();
+ return await client.ToHttpResponseMessageAsync(responseFactory).ConfigureAwait(false);
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/SelfManagedWebHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/SelfManagedWebHostFixture.cs
index 0aa9818..5b90e9a 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/SelfManagedWebHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/SelfManagedWebHostFixture.cs
@@ -1,21 +1,20 @@
using System.Threading.Tasks;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Represents a self-managed implementation of the class.
+///
+public sealed class SelfManagedWebHostFixture : ManagedWebHostFixture
{
///
- /// Represents a self-managed implementation of the class.
+ /// Initializes a new instance of the class.
///
- public sealed class SelfManagedWebHostFixture : ManagedWebHostFixture
+ ///
+ /// This constructor sets the to a no-op asynchronous delegate.
+ ///
+ public SelfManagedWebHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- ///
- /// This constructor sets the to a no-op asynchronous delegate.
- ///
- public SelfManagedWebHostFixture()
- {
- AsyncHostRunnerCallback = (_, __) => Task.CompletedTask;
- }
+ AsyncHostRunnerCallback = (_, __) => Task.CompletedTask;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/SelfManagedWebMinimalHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/SelfManagedWebMinimalHostFixture.cs
index 6b94eb3..8cefce7 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/SelfManagedWebMinimalHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/SelfManagedWebMinimalHostFixture.cs
@@ -1,21 +1,20 @@
using System.Threading.Tasks;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Represents a self-managed implementation of the class.
+///
+public sealed class SelfManagedWebMinimalHostFixture : ManagedWebMinimalHostFixture
{
///
- /// Represents a self-managed implementation of the class.
+ /// Initializes a new instance of the class.
///
- public sealed class SelfManagedWebMinimalHostFixture : ManagedWebMinimalHostFixture
+ ///
+ /// This constructor sets the to a no-op asynchronous delegate.
+ ///
+ public SelfManagedWebMinimalHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- ///
- /// This constructor sets the to a no-op asynchronous delegate.
- ///
- public SelfManagedWebMinimalHostFixture()
- {
- AsyncHostRunnerCallback = (_, __) => Task.CompletedTask;
- }
+ AsyncHostRunnerCallback = (_, __) => Task.CompletedTask;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ServiceCollectionExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ServiceCollectionExtensions.cs
index c8b6978..9e1db52 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ServiceCollectionExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/ServiceCollectionExtensions.cs
@@ -5,41 +5,40 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Extension methods for the interface.
+///
+public static class ServiceCollectionExtensions
{
///
- /// Extension methods for the interface.
+ /// Adds a unit test optimized implementation for the service.
///
- public static class ServiceCollectionExtensions
+ /// The to extend.
+ /// The lifetime of the service. Default is .
+ /// A reference to after the operation has completed.
+ public static IServiceCollection AddFakeHttpContextAccessor(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
- ///
- /// Adds a unit test optimized implementation for the service.
- ///
- /// The to extend.
- /// The lifetime of the service. Default is .
- /// A reference to after the operation has completed.
- public static IServiceCollection AddFakeHttpContextAccessor(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ switch (lifetime)
{
- switch (lifetime)
- {
- case ServiceLifetime.Transient:
- services.TryAddTransient(FakeHttpContextAccessorFactory);
- break;
- case ServiceLifetime.Scoped:
- services.TryAddScoped(FakeHttpContextAccessorFactory);
- break;
- case ServiceLifetime.Singleton:
- services.TryAddSingleton(FakeHttpContextAccessorFactory);
- break;
- default:
- throw new InvalidEnumArgumentException(nameof(lifetime), (int)lifetime, typeof(ServiceLifetime));
- }
- return services;
+ case ServiceLifetime.Transient:
+ services.TryAddTransient(FakeHttpContextAccessorFactory);
+ break;
+ case ServiceLifetime.Scoped:
+ services.TryAddScoped(FakeHttpContextAccessorFactory);
+ break;
+ case ServiceLifetime.Singleton:
+ services.TryAddSingleton(FakeHttpContextAccessorFactory);
+ break;
+ default:
+ throw new InvalidEnumArgumentException(nameof(lifetime), (int)lifetime, typeof(ServiceLifetime));
}
+ return services;
+ }
- private static IHttpContextAccessor FakeHttpContextAccessorFactory(IServiceProvider provider)
- {
- return new FakeHttpContextAccessor(provider.GetRequiredService());
- }
+ private static IHttpContextAccessor FakeHttpContextAccessorFactory(IServiceProvider provider)
+ {
+ return new FakeHttpContextAccessor(provider.GetRequiredService());
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Tweaker.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Tweaker.cs
index 83e3a7a..116c425 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Tweaker.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Tweaker.cs
@@ -1,12 +1,11 @@
using System;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+internal static class Tweaker
{
- internal static class Tweaker
+ internal static T Adjust(T value, Func converter)
{
- internal static T Adjust(T value, Func converter)
- {
- return converter == null ? value : converter.Invoke(value);
- }
+ return converter == null ? value : converter.Invoke(value);
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostFixtureExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostFixtureExtensions.cs
index 246aa40..c589bfa 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostFixtureExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostFixtureExtensions.cs
@@ -1,23 +1,22 @@
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Extension methods for the interface.
+///
+public static class WebHostFixtureExtensions
{
///
- /// Extension methods for the interface.
+ /// Determines whether the specified has a valid state.
///
- public static class WebHostFixtureExtensions
+ /// The to check.
+ /// true if the specified has a valid state; otherwise, false .
+ ///
+ /// A valid state is defined as having non-null values for the following properties:
+ /// , and .
+ ///
+ public static bool HasValidState(this IWebHostFixture hostFixture)
{
- ///
- /// Determines whether the specified has a valid state.
- ///
- /// The to check.
- /// true if the specified has a valid state; otherwise, false .
- ///
- /// A valid state is defined as having non-null values for the following properties:
- /// , and .
- ///
- public static bool HasValidState(this IWebHostFixture hostFixture)
- {
- var hasValidState = ((IGenericHostFixture)hostFixture).HasValidState();
- return hasValidState && hostFixture.ConfigureApplicationCallback != null;
- }
+ var hasValidState = ((IGenericHostFixture)hostFixture).HasValidState();
+ return hasValidState && hostFixture.ConfigureApplicationCallback != null;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostTest.cs
index b7b6b99..7a89a13 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostTest.cs
@@ -3,60 +3,59 @@
using Microsoft.Extensions.Hosting;
using Xunit;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection and depends on ASP.NET Core, should derive.
+///
+/// The type of the object that implements the interface.
+///
+///
+public abstract class WebHostTest : HostTest, IWebHostTest where T : class, IWebHostFixture
{
///
- /// Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection and depends on ASP.NET Core, should derive.
+ /// Initializes a new instance of the class.
///
- /// The type of the object that implements the interface.
- ///
- ///
- public abstract class WebHostTest : HostTest, IWebHostTest where T : class, IWebHostFixture
+ /// An implementation of the interface.
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ protected WebHostTest(T hostFixture, ITestOutputHelper output = null, Type callerType = null) : this(false, hostFixture, output, callerType)
{
- ///
- /// Initializes a new instance of the class.
- ///
- /// An implementation of the interface.
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- protected WebHostTest(T hostFixture, ITestOutputHelper output = null, Type callerType = null) : this(false, hostFixture, output, callerType)
- {
- }
+ }
- ///
- /// Initializes a new instance of the class.
- ///
- /// A value indicating whether to skip the host fixture initialization.
- /// An implementation of the interface.
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- protected WebHostTest(bool skipHostFixtureInitialization, T hostFixture, ITestOutputHelper output = null, Type callerType = null) : base(skipHostFixtureInitialization, hostFixture, output, callerType)
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// A value indicating whether to skip the host fixture initialization.
+ /// An implementation of the interface.
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ protected WebHostTest(bool skipHostFixtureInitialization, T hostFixture, ITestOutputHelper output = null, Type callerType = null) : base(skipHostFixtureInitialization, hostFixture, output, callerType)
+ {
+ ArgumentNullException.ThrowIfNull(hostFixture);
+ if (skipHostFixtureInitialization) { return; }
+ if (!hostFixture.HasValidState())
{
- ArgumentNullException.ThrowIfNull(hostFixture);
- if (skipHostFixtureInitialization) { return; }
- if (!hostFixture.HasValidState())
- {
- hostFixture.ConfigureHostCallback = ConfigureHost;
- hostFixture.ConfigureCallback = Configure;
- hostFixture.ConfigureServicesCallback = ConfigureServices;
- hostFixture.ConfigureApplicationCallback = ConfigureApplication;
- hostFixture.ConfigureHost(this);
- }
- Host = hostFixture.Host;
- Application = hostFixture.Application;
- Configure(hostFixture.Configuration, hostFixture.Environment);
+ hostFixture.ConfigureHostCallback = ConfigureHost;
+ hostFixture.ConfigureCallback = Configure;
+ hostFixture.ConfigureServicesCallback = ConfigureServices;
+ hostFixture.ConfigureApplicationCallback = ConfigureApplication;
+ hostFixture.ConfigureHost(this);
}
+ Host = hostFixture.Host;
+ Application = hostFixture.Application;
+ Configure(hostFixture.Configuration, hostFixture.Environment);
+ }
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- public IApplicationBuilder Application { get; protected set; }
+ ///
+ /// Gets the initialized by the .
+ ///
+ /// The initialized by the .
+ public IApplicationBuilder Application { get; protected set; }
- ///
- /// Configures the HTTP request pipeline.
- ///
- /// The type that provides the mechanisms to configure the HTTP request pipeline.
- public abstract void ConfigureApplication(IApplicationBuilder app);
- }
+ ///
+ /// Configures the HTTP request pipeline.
+ ///
+ /// The type that provides the mechanisms to configure the HTTP request pipeline.
+ public abstract void ConfigureApplication(IApplicationBuilder app);
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostTestFactory.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostTestFactory.cs
index 68131f8..4904d93 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostTestFactory.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebHostTestFactory.cs
@@ -7,68 +7,67 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Provides a set of static methods for ASP.NET Core (including, but not limited to MVC, Razor and related) unit testing.
+///
+/// .
+public static class WebHostTestFactory
{
///
- /// Provides a set of static methods for ASP.NET Core (including, but not limited to MVC, Razor and related) unit testing.
+ /// Creates and returns an implementation.
///
- /// .
- public static class WebHostTestFactory
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The which may be configured.
+ /// An optional implementation to use instead of the default instance.
+ /// An instance of an implementation.
+ public static IWebHostTest Create(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, IWebHostFixture hostFixture = null)
{
- ///
- /// Creates and returns an implementation.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// The which may be configured.
- /// An optional implementation to use instead of the default instance.
- /// An instance of an implementation.
- public static IWebHostTest Create(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, IWebHostFixture hostFixture = null)
- {
- return new WebHostTest(serviceSetup, pipelineSetup, hostSetup, hostFixture ?? new ManagedWebHostFixture());
- }
+ return new WebHostTest(serviceSetup, pipelineSetup, hostSetup, hostFixture ?? new ManagedWebHostFixture());
+ }
- ///
- /// Creates and returns an implementation.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// The which may be configured.
- /// An optional implementation to use instead of the default instance.
- /// An instance of an implementation.
- public static IWebHostTest CreateWithHostBuilderContext(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, IWebHostFixture hostFixture = null)
- {
- return new WebHostTest(serviceSetup, pipelineSetup, hostSetup, hostFixture ?? new ManagedWebHostFixture());
- }
+ ///
+ /// Creates and returns an implementation.
+ ///
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The which may be configured.
+ /// An optional implementation to use instead of the default instance.
+ /// An instance of an implementation.
+ public static IWebHostTest CreateWithHostBuilderContext(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, IWebHostFixture hostFixture = null)
+ {
+ return new WebHostTest(serviceSetup, pipelineSetup, hostSetup, hostFixture ?? new ManagedWebHostFixture());
+ }
- ///
- /// Runs a middleware and returns an for making HTTP requests to the test server.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// The which may be configured.
- /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
- /// An optional implementation to use instead of the default instance.
- /// A that represents the asynchronous operation. The task result contains the for the test server.
- public static async Task RunAsync(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, Func> responseFactory = null, IWebHostFixture hostFixture = null)
- {
- using var client = Create(serviceSetup, pipelineSetup, hostSetup, hostFixture).Host.GetTestClient();
- return await client.ToHttpResponseMessageAsync(responseFactory).ConfigureAwait(false);
- }
+ ///
+ /// Runs a middleware and returns an for making HTTP requests to the test server.
+ ///
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
+ /// An optional implementation to use instead of the default instance.
+ /// A that represents the asynchronous operation. The task result contains the for the test server.
+ public static async Task RunAsync(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, Func> responseFactory = null, IWebHostFixture hostFixture = null)
+ {
+ using var client = Create(serviceSetup, pipelineSetup, hostSetup, hostFixture).Host.GetTestClient();
+ return await client.ToHttpResponseMessageAsync(responseFactory).ConfigureAwait(false);
+ }
- ///
- /// Runs a filter/middleware test.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// The which may be configured.
- /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
- /// An optional implementation to use instead of the default instance.
- /// A that represents the asynchronous operation. The task result contains the for the test server.
- public static async Task RunWithHostBuilderContextAsync(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, Func> responseFactory = null, IWebHostFixture hostFixture = null)
- {
- using var client = CreateWithHostBuilderContext(serviceSetup, pipelineSetup, hostSetup, hostFixture).Host.GetTestClient();
- return await client.ToHttpResponseMessageAsync(responseFactory).ConfigureAwait(false);
- }
+ ///
+ /// Runs a filter/middleware test.
+ ///
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The which may be configured.
+ /// The function delegate that creates a from the . Default is a GET request to the root URL ("/").
+ /// An optional implementation to use instead of the default instance.
+ /// A that represents the asynchronous operation. The task result contains the for the test server.
+ public static async Task RunWithHostBuilderContextAsync(Action serviceSetup = null, Action pipelineSetup = null, Action hostSetup = null, Func> responseFactory = null, IWebHostFixture hostFixture = null)
+ {
+ using var client = CreateWithHostBuilderContext(serviceSetup, pipelineSetup, hostSetup, hostFixture).Host.GetTestClient();
+ return await client.ToHttpResponseMessageAsync(responseFactory).ConfigureAwait(false);
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebMinimalHostFixtureExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebMinimalHostFixtureExtensions.cs
index ce03641..de3562f 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebMinimalHostFixtureExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebMinimalHostFixtureExtensions.cs
@@ -1,24 +1,23 @@
-namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore
+namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore;
+
+///
+/// Extension methods for the interface.
+///
+public static class WebMinimalHostFixtureExtensions
{
///
- /// Extension methods for the interface.
+ /// Determines whether the specified has a valid state.
///
- public static class WebMinimalHostFixtureExtensions
+ /// The to check.
+ /// true if the specified has a valid state; otherwise, false .
+ ///
+ /// A valid state is defined as having non-null values for the following properties:
+ /// , ,
+ /// , and .
+ ///
+ public static bool HasValidState(this IWebMinimalHostFixture hostFixture)
{
- ///
- /// Determines whether the specified has a valid state.
- ///
- /// The to check.
- /// true if the specified has a valid state; otherwise, false .
- ///
- /// A valid state is defined as having non-null values for the following properties:
- /// , ,
- /// , and .
- ///
- public static bool HasValidState(this IWebMinimalHostFixture hostFixture)
- {
- var hasValidState = ((IMinimalHostFixture)hostFixture).HasValidState();
- return hasValidState && hostFixture.ConfigureApplicationCallback != null && hostFixture.Application != null;
- }
+ var hasValidState = ((IMinimalHostFixture)hostFixture).HasValidState();
+ return hasValidState && hostFixture.ConfigureApplicationCallback != null && hostFixture.Application != null;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/GenericHostFixtureExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting/GenericHostFixtureExtensions.cs
index 373f8ef..32f110a 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/GenericHostFixtureExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/GenericHostFixtureExtensions.cs
@@ -1,23 +1,22 @@
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Extension methods for the interface.
+///
+public static class GenericHostFixtureExtensions
{
///
- /// Extension methods for the interface.
+ /// Determines whether the specified has a valid state.
///
- public static class GenericHostFixtureExtensions
+ /// The to check.
+ /// true if the specified has a valid state; otherwise, false .
+ ///
+ /// A valid state is defined as having non-null values for the following properties:
+ /// and .
+ ///
+ public static bool HasValidState(this IGenericHostFixture hostFixture)
{
- ///
- /// Determines whether the specified has a valid state.
- ///
- /// The to check.
- /// true if the specified has a valid state; otherwise, false .
- ///
- /// A valid state is defined as having non-null values for the following properties:
- /// and .
- ///
- public static bool HasValidState(this IGenericHostFixture hostFixture)
- {
- return hostFixture.ConfigureServicesCallback != null &&
- hostFixture.ConfigureHostCallback != null;
- }
+ return hostFixture.ConfigureServicesCallback != null &&
+ hostFixture.ConfigureHostCallback != null;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/GlobalSuppressions.cs b/src/Codebelt.Extensions.Xunit.Hosting/GlobalSuppressions.cs
index c2de754..a73e32e 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/GlobalSuppressions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/GlobalSuppressions.cs
@@ -1,4 +1,4 @@
-// This file is used by Code Analysis to maintain SuppressMessage
+// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
@@ -7,13 +7,13 @@
[assembly: SuppressMessage("Major Code Smell", "S3881:\"IDisposable\" should be implemented correctly", Justification = "This is an implementation of the IDisposable interface tailored to avoid wrong implementations.", Scope = "type", Target = "~T:Codebelt.Extensions.Xunit.Hosting.HostFixture")]
[assembly: SuppressMessage("Major Code Smell", "S3971:\"GC.SuppressFinalize\" should not be called", Justification = "False-Positive due to IAsyncDisposable living side-by-side with IDisposable.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.HostFixture.DisposeAsync~System.Threading.Tasks.ValueTask")]
-[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.GenericHostTestFactory.Create(System.Action{Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},Codebelt.Extensions.Xunit.Hosting.IHostFixture)~Codebelt.Extensions.Xunit.Hosting.IGenericHostTest")]
-[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.GenericHostTestFactory.CreateWithHostBuilderContext(System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},Codebelt.Extensions.Xunit.Hosting.IHostFixture)~Codebelt.Extensions.Xunit.Hosting.IGenericHostTest")]
-[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.HostTest`1.#ctor(System.Boolean,`0,Xunit.Abstractions.ITestOutputHelper,System.Type)")]
+[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.HostTestFactory.Create(System.Action{Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},Codebelt.Extensions.Xunit.Hosting.IGenericHostFixture)~Codebelt.Extensions.Xunit.Hosting.IHostTest")]
+[assembly: SuppressMessage("Blocker Code Smell", "S3427:Method overloads with default parameter values should not overlap", Justification = "Avoid bumping major version by providing an extra overloaded member as optional argument.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.HostTestFactory.CreateWithHostBuilderContext(System.Action{Microsoft.Extensions.Hosting.HostBuilderContext,Microsoft.Extensions.DependencyInjection.IServiceCollection},System.Action{Microsoft.Extensions.Hosting.IHostBuilder},Codebelt.Extensions.Xunit.Hosting.IGenericHostFixture)~Codebelt.Extensions.Xunit.Hosting.IHostTest")]
+[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.HostTest`1.#ctor(System.Boolean,`0,Xunit.ITestOutputHelper,System.Type)")]
[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.LoggerExtensions.GetTestStore(Microsoft.Extensions.Logging.ILogger,System.String)~Codebelt.Extensions.Xunit.ITestStore{Codebelt.Extensions.Xunit.Hosting.XunitTestLoggerEntry}")]
[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.ManagedHostFixture.ConfigureHost(Codebelt.Extensions.Xunit.Test)")]
[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.ManagedMinimalHostFixture.ConfigureHost(Codebelt.Extensions.Xunit.Test)")]
-[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.MinimalHostTest`1.#ctor(System.Boolean,`0,Xunit.Abstractions.ITestOutputHelper,System.Type)")]
+[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.MinimalHostTest`1.#ctor(System.Boolean,`0,Xunit.ITestOutputHelper,System.Type)")]
[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.ServiceCollectionExtensions.AddXunitTestLogging(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Logging.LogLevel)~Microsoft.Extensions.DependencyInjection.IServiceCollection")]
-[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.ServiceCollectionExtensions.AddXunitTestLogging(Microsoft.Extensions.DependencyInjection.IServiceCollection,Xunit.Abstractions.ITestOutputHelper,Microsoft.Extensions.Logging.LogLevel)~Microsoft.Extensions.DependencyInjection.IServiceCollection")]
+[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.ServiceCollectionExtensions.AddXunitTestLogging(Microsoft.Extensions.DependencyInjection.IServiceCollection,Xunit.ITestOutputHelper,Microsoft.Extensions.Logging.LogLevel)~Microsoft.Extensions.DependencyInjection.IServiceCollection")]
[assembly: SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Wont fix. Multi-TFMs. Only supported with >= .NET 7.", Scope = "member", Target = "~M:Codebelt.Extensions.Xunit.Hosting.ServiceCollectionExtensions.AddXunitTestLoggingOutputHelperAccessor``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)~Microsoft.Extensions.DependencyInjection.IServiceCollection")]
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/HostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting/HostFixture.cs
index 461917b..0abf8f9 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/HostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/HostFixture.cs
@@ -5,194 +5,193 @@
using Microsoft.Extensions.Hosting;
using Xunit;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Represents the base class from which all implementations of xUnit fixture concept should derive.
+///
+///
+public abstract class HostFixture : IHostFixture, IAsyncLifetime
{
- ///
- /// Represents the base class from which all implementations of xUnit fixture concept should derive.
- ///
- ///
- public abstract class HostFixture : IHostFixture, IAsyncLifetime
- {
#if NET9_0_OR_GREATER
- private readonly Lock _lock = new();
+ private readonly Lock _lock = new();
#else
- private readonly object _lock = new();
+ private readonly object _lock = new();
#endif
- private Func _asyncHostRunnerCallback = async (host, cancellationToken) =>
+ private Func _asyncHostRunnerCallback = async (host, cancellationToken) =>
+ {
+ if (SynchronizationContext.Current == null)
+ {
+ await host.StartAsync(cancellationToken).ConfigureAwait(false);
+ }
+ else
{
- if (SynchronizationContext.Current == null)
+ Task.Run(async () =>
{
await host.StartAsync(cancellationToken).ConfigureAwait(false);
- }
- else
- {
- Task.Run(async () =>
- {
- await host.StartAsync(cancellationToken).ConfigureAwait(false);
- }).GetAwaiter().GetResult();
- }
+ }).GetAwaiter().GetResult();
+ }
- // this was done to reduce the risk of deadlocks (https://stackoverflow.com/questions/50918647/why-does-this-xunit-test-deadlock-on-a-single-cpu-vm/50953607#50953607)
- };
+ // this was done to reduce the risk of deadlocks (https://stackoverflow.com/questions/50918647/why-does-this-xunit-test-deadlock-on-a-single-cpu-vm/50953607#50953607)
+ };
- ///
- /// Initializes a new instance of the class.
- ///
- protected HostFixture()
- {
- }
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ protected HostFixture()
+ {
+ }
- ///
- /// Determines whether the specified contains one or more of the specified target .
- ///
- /// The to validate.
- /// The target types to be matched against.
- /// true if the contains one or more of the specified target types; otherwise, false .
- protected static bool HasTypes(Type type, params Type[] types)
+ ///
+ /// Determines whether the specified contains one or more of the specified target .
+ ///
+ /// The to validate.
+ /// The target types to be matched against.
+ /// true if the contains one or more of the specified target types; otherwise, false .
+ protected static bool HasTypes(Type type, params Type[] types)
+ {
+ foreach (var tt in types)
{
- foreach (var tt in types)
+ var st = type;
+ while (st != null)
{
- var st = type;
- while (st != null)
- {
- if (st.IsGenericType && tt == st.GetGenericTypeDefinition()) { return true; }
- if (st == tt) { return true; }
- st = st.BaseType;
- }
+ if (st.IsGenericType && tt == st.GetGenericTypeDefinition()) { return true; }
+ if (st == tt) { return true; }
+ st = st.BaseType;
}
- return false;
}
+ return false;
+ }
- ///
- /// Gets or sets the delegate responsible for running the .
- ///
- /// The delegate responsible for running the .
- ///
- /// cannot be null.
- ///
- protected Func AsyncHostRunnerCallback
+ ///
+ /// Gets or sets the delegate responsible for running the .
+ ///
+ /// The delegate responsible for running the .
+ ///
+ /// cannot be null.
+ ///
+ protected Func AsyncHostRunnerCallback
+ {
+ get => _asyncHostRunnerCallback;
+ set => _asyncHostRunnerCallback = value ?? throw new ArgumentNullException(nameof(value), "The host runner delegate cannot be null.");
+ }
+
+ ///
+ /// Gets or sets the delegate that initializes the test class.
+ ///
+ /// The delegate that initializes the test class.
+ /// Mimics the Startup convention.
+ public Action ConfigureCallback { get; set; }
+
+ ///
+ /// Gets or sets the initialized by this instance.
+ ///
+ /// The initialized by this instance.
+ public IHost Host { get; protected set; }
+
+ ///
+ /// Gets the initialized by this instance.
+ ///
+ /// The initialized by this instance.
+ public IConfiguration Configuration { get; protected set; }
+
+ ///
+ /// Gets the initialized by this instance.
+ ///
+ /// The initialized by this instance.
+ public IHostEnvironment Environment { get; protected set; }
+
+ ///
+ /// Gets a value indicating whether this object is disposed.
+ ///
+ /// true if this object is disposed; otherwise, false .
+ public bool Disposed { get; private set; }
+
+ ///
+ /// Called when this object is being disposed by either or having disposing set to true and is false .
+ ///
+ protected virtual void OnDisposeManagedResources()
+ {
+ Host?.Dispose();
+ }
+
+ ///
+ /// Called when this object is being disposed by .
+ ///
+#if NET8_0_OR_GREATER
+ protected virtual async ValueTask OnDisposeManagedResourcesAsync()
+ {
+ if (Host is IAsyncDisposable asyncDisposable)
{
- get => _asyncHostRunnerCallback;
- set => _asyncHostRunnerCallback = value ?? throw new ArgumentNullException(nameof(value), "The host runner delegate cannot be null.");
+ await asyncDisposable.DisposeAsync();
}
-
- ///
- /// Gets or sets the delegate that initializes the test class.
- ///
- /// The delegate that initializes the test class.
- /// Mimics the Startup convention.
- public Action ConfigureCallback { get; set; }
-
- ///
- /// Gets or sets the initialized by this instance.
- ///
- /// The initialized by this instance.
- public IHost Host { get; protected set; }
-
- ///
- /// Gets the initialized by this instance.
- ///
- /// The initialized by this instance.
- public IConfiguration Configuration { get; protected set; }
-
- ///
- /// Gets the initialized by this instance.
- ///
- /// The initialized by this instance.
- public IHostEnvironment Environment { get; protected set; }
-
- ///
- /// Gets a value indicating whether this object is disposed.
- ///
- /// true if this object is disposed; otherwise, false .
- public bool Disposed { get; private set; }
-
- ///
- /// Called when this object is being disposed by either or having disposing set to true and is false .
- ///
- protected virtual void OnDisposeManagedResources()
+ else
{
Host?.Dispose();
}
-
- ///
- /// Called when this object is being disposed by .
- ///
-#if NET8_0_OR_GREATER
- protected virtual async ValueTask OnDisposeManagedResourcesAsync()
+ }
+#else
+ protected virtual ValueTask OnDisposeManagedResourcesAsync()
{
- if (Host is IAsyncDisposable asyncDisposable)
- {
- await asyncDisposable.DisposeAsync();
- }
- else
- {
- Host?.Dispose();
- }
+ OnDisposeManagedResources();
+ return default;
}
-#else
- protected virtual ValueTask OnDisposeManagedResourcesAsync()
- {
- OnDisposeManagedResources();
- return default;
- }
#endif
- ///
- /// Called when this object is being disposed by either or and is false .
- ///
- protected virtual void OnDisposeUnmanagedResources()
- {
- }
+ ///
+ /// Called when this object is being disposed by either or and is false .
+ ///
+ protected virtual void OnDisposeUnmanagedResources()
+ {
+ }
- ///
- /// Releases all resources used by the object.
- ///
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
+ ///
+ /// Releases all resources used by the object.
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
- ///
- /// Releases the unmanaged resources used by the object and optionally releases the managed resources.
- ///
- /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
- protected void Dispose(bool disposing)
+ ///
+ /// Releases the unmanaged resources used by the object and optionally releases the managed resources.
+ ///
+ /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
+ protected void Dispose(bool disposing)
+ {
+ if (Disposed) { return; }
+ lock (_lock)
{
if (Disposed) { return; }
- lock (_lock)
+ if (disposing)
{
- if (Disposed) { return; }
- if (disposing)
- {
- OnDisposeManagedResources();
- }
- OnDisposeUnmanagedResources();
- Disposed = true;
+ OnDisposeManagedResources();
}
+ OnDisposeUnmanagedResources();
+ Disposed = true;
}
+ }
- ///
- /// Asynchronously releases the resources used by the .
- ///
- /// A that represents the asynchronous dispose operation.
- /// https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#the-disposeasync-method
- public async ValueTask DisposeAsync()
- {
- await OnDisposeManagedResourcesAsync().ConfigureAwait(false);
- Dispose(false);
- GC.SuppressFinalize(this);
- }
+ ///
+ /// Asynchronously releases the resources used by the .
+ ///
+ /// A that represents the asynchronous dispose operation.
+ /// https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#the-disposeasync-method
+ public async ValueTask DisposeAsync()
+ {
+ await OnDisposeManagedResourcesAsync().ConfigureAwait(false);
+ Dispose(false);
+ GC.SuppressFinalize(this);
+ }
- ///
- /// Called immediately after the class has been created, before it is used.
- ///
- /// A that represents the asynchronous operation.
- public virtual ValueTask InitializeAsync()
- {
- return default;
- }
+ ///
+ /// Called immediately after the class has been created, before it is used.
+ ///
+ /// A that represents the asynchronous operation.
+ public virtual ValueTask InitializeAsync()
+ {
+ return default;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/HostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting/HostTest.cs
index 3e2cf9f..18b7f19 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/HostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/HostTest.cs
@@ -1,118 +1,121 @@
-using System;
+using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Represents the non-generic base class from where its generic equivalent should derive.
+///
+///
+///
+///
+public abstract class HostTest : Test, IHostTest
{
///
- /// Represents the non-generic base class from where its generic equivalent should derive.
+ /// Initializes a new instance of the class.
///
- ///
- ///
- ///
- public abstract class HostTest : Test, IHostTest
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ protected HostTest(ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
{
- ///
- /// Initializes a new instance of the class.
- ///
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- protected HostTest(ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
- {
- }
+ }
- ///
- /// Adds and to this instance.
- ///
- /// The initialized by the .
- /// The initialized by the .
- public virtual void Configure(IConfiguration configuration, IHostEnvironment environment)
- {
- Configuration = configuration;
- Environment = environment;
- }
+ ///
+ /// Adds and to this instance.
+ ///
+ /// The initialized by the .
+ /// The initialized by the .
+ public virtual void Configure(IConfiguration configuration, IHostEnvironment environment)
+ {
+ Configuration = configuration;
+ Environment = environment;
+ }
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- public IHost Host { get; protected set; }
+ ///
+ /// Gets the initialized by the .
+ ///
+ /// The initialized by the .
+ public IHost Host { get; protected set; }
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- public IConfiguration Configuration { get; protected set; }
+ ///
+ /// Gets the initialized by the .
+ ///
+ /// The initialized by the .
+ public IConfiguration Configuration { get; protected set; }
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- public IHostEnvironment Environment { get; protected set; }
- }
+ ///
+ /// Gets the initialized by the .
+ ///
+ /// The initialized by the .
+ public IHostEnvironment Environment { get; protected set; }
+}
+///
+/// Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection, should derive.
+///
+/// The type of the object that implements the interface.
+///
+///
+///
+/// The class needed to be designed in this rather complex way, as this is the only way that xUnit supports a shared context. The need for shared context is theoretical at best, but it does opt-in for Scoped instances.
+public abstract class HostTest : HostTest, IClassFixture where T : class, IGenericHostFixture
+{
///
- /// Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection, should derive.
+ /// Initializes a new instance of the class.
///
- /// The type of the object that implements the interface.
- ///
- ///
- ///
- /// The class needed to be designed in this rather complex way, as this is the only way that xUnit supports a shared context. The need for shared context is theoretical at best, but it does opt-in for Scoped instances.
- public abstract class HostTest : HostTest, IClassFixture where T : class, IGenericHostFixture
+ /// An implementation of the interface.
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ ///
+ /// is null.
+ ///
+ protected HostTest(T hostFixture, ITestOutputHelper output = null, Type callerType = null) : this(false, hostFixture, output, callerType)
{
- ///
- /// Initializes a new instance of the class.
- ///
- /// An implementation of the interface.
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- ///
- /// is null.
- ///
- protected HostTest(T hostFixture, ITestOutputHelper output = null, Type callerType = null) : this(false, hostFixture, output, callerType)
- {
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// A value indicating whether to skip the host fixture initialization.
- /// An implementation of the interface.
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- ///
- /// is null.
- ///
- protected HostTest(bool skipHostFixtureInitialization, T hostFixture, ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
- {
- if (hostFixture == null) { throw new ArgumentNullException(nameof(hostFixture)); }
- if (skipHostFixtureInitialization) { return; }
- if (!hostFixture.HasValidState())
- {
- hostFixture.ConfigureHostCallback = ConfigureHost;
- hostFixture.ConfigureCallback = Configure;
- hostFixture.ConfigureServicesCallback = ConfigureServices;
- hostFixture.ConfigureHost(this);
- }
- Host = hostFixture.Host;
- Configure(hostFixture.Configuration, hostFixture.Environment);
- }
+ }
- ///
- /// Provides a way to override the defaults set up by .
- ///
- /// The that initializes an instance of .
- protected virtual void ConfigureHost(IHostBuilder hb)
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// A value indicating whether to skip the host fixture initialization.
+ /// An implementation of the interface.
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ ///
+ /// is null.
+ ///
+ protected HostTest(bool skipHostFixtureInitialization, T hostFixture, ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
+ {
+#if NETSTANDARD2_0
+ if (hostFixture == null) { throw new ArgumentNullException(nameof(hostFixture)); }
+#else
+ ArgumentNullException.ThrowIfNull(hostFixture);
+#endif
+ if (skipHostFixtureInitialization) { return; }
+ if (!hostFixture.HasValidState())
{
+ hostFixture.ConfigureHostCallback = ConfigureHost;
+ hostFixture.ConfigureCallback = Configure;
+ hostFixture.ConfigureServicesCallback = ConfigureServices;
+ hostFixture.ConfigureHost(this);
}
+ Host = hostFixture.Host;
+ Configure(hostFixture.Configuration, hostFixture.Environment);
+ }
- ///
- /// Adds services to the container.
- ///
- /// The collection of service descriptors.
- public abstract void ConfigureServices(IServiceCollection services);
+ ///
+ /// Provides a way to override the defaults set up by .
+ ///
+ /// The that initializes an instance of .
+ protected virtual void ConfigureHost(IHostBuilder hb)
+ {
}
+
+ ///
+ /// Adds services to the container.
+ ///
+ /// The collection of service descriptors.
+ public abstract void ConfigureServices(IServiceCollection services);
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/HostTestFactory.cs b/src/Codebelt.Extensions.Xunit.Hosting/HostTestFactory.cs
index 0962eea..fb4dd21 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/HostTestFactory.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/HostTestFactory.cs
@@ -2,35 +2,34 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Provides a set of static methods for unit testing.
+///
+public static class HostTestFactory
{
///
- /// Provides a set of static methods for unit testing.
+ /// Creates and returns an implementation.
///
- public static class HostTestFactory
+ /// The which may be configured.
+ /// The which may be configured.
+ /// An optional implementation to use instead of the default instance.
+ /// An instance of an implementation.
+ public static IHostTest Create(Action serviceSetup = null, Action hostSetup = null, IGenericHostFixture hostFixture = null)
{
- ///
- /// Creates and returns an implementation.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// An optional implementation to use instead of the default instance.
- /// An instance of an implementation.
- public static IHostTest Create(Action serviceSetup = null, Action hostSetup = null, IGenericHostFixture hostFixture = null)
- {
- return new Internal.HostTest(serviceSetup, hostSetup, hostFixture ?? new ManagedHostFixture());
- }
+ return new Internal.HostTest(serviceSetup, hostSetup, hostFixture ?? new ManagedHostFixture());
+ }
- ///
- /// Creates and returns an implementation.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// An optional implementation to use instead of the default instance.
- /// An instance of an implementation.
- public static IHostTest CreateWithHostBuilderContext(Action serviceSetup = null, Action hostSetup = null, IGenericHostFixture hostFixture = null)
- {
- return new Internal.HostTest(serviceSetup, hostSetup, hostFixture ?? new ManagedHostFixture());
- }
+ ///
+ /// Creates and returns an implementation.
+ ///
+ /// The which may be configured.
+ /// The which may be configured.
+ /// An optional implementation to use instead of the default instance.
+ /// An instance of an implementation.
+ public static IHostTest CreateWithHostBuilderContext(Action serviceSetup = null, Action hostSetup = null, IGenericHostFixture hostFixture = null)
+ {
+ return new Internal.HostTest(serviceSetup, hostSetup, hostFixture ?? new ManagedHostFixture());
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/IConfigurationTest.cs b/src/Codebelt.Extensions.Xunit.Hosting/IConfigurationTest.cs
index 6a63ae6..e925a28 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/IConfigurationTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/IConfigurationTest.cs
@@ -1,17 +1,16 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Represents the members needed for DI testing with support for Configuration.
+///
+public interface IConfigurationTest
{
///
- /// Represents the members needed for DI testing with support for Configuration.
+ /// Gets the initialized by the .
///
- public interface IConfigurationTest
- {
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- IConfiguration Configuration { get; }
- }
+ /// The initialized by the .
+ IConfiguration Configuration { get; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/IEnvironmentTest.cs b/src/Codebelt.Extensions.Xunit.Hosting/IEnvironmentTest.cs
index 95c3a22..6801a74 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/IEnvironmentTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/IEnvironmentTest.cs
@@ -1,16 +1,15 @@
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Represents the members needed for DI testing with support for .
+///
+public interface IEnvironmentTest
{
///
- /// Represents the members needed for DI testing with support for .
+ /// Gets the initialized by the .
///
- public interface IEnvironmentTest
- {
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- IHostEnvironment Environment { get; }
- }
+ /// The initialized by the .
+ IHostEnvironment Environment { get; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/IGenericHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting/IGenericHostFixture.cs
index 92ce0d8..457c579 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/IGenericHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/IGenericHostFixture.cs
@@ -2,31 +2,30 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Provides a way to use Microsoft Dependency Injection in unit tests.
+///
+///
+public interface IGenericHostFixture : IHostFixture
{
///
- /// Provides a way to use Microsoft Dependency Injection in unit tests.
+ /// Gets or sets the delegate that adds services to the container.
///
- ///
- public interface IGenericHostFixture : IHostFixture
- {
- ///
- /// Gets or sets the delegate that adds services to the container.
- ///
- /// The delegate that adds services to the container.
- Action ConfigureServicesCallback { get; set; }
+ /// The delegate that adds services to the container.
+ Action ConfigureServicesCallback { get; set; }
- ///
- /// Gets or sets the delegate that provides a way to override the defaults set up by .
- ///
- /// The delegate that provides a way to override the .
- Action ConfigureHostCallback { get; set; }
+ ///
+ /// Gets or sets the delegate that provides a way to override the defaults set up by .
+ ///
+ /// The delegate that provides a way to override the .
+ Action ConfigureHostCallback { get; set; }
- ///
- /// Creates and configures the of this .
- ///
- /// The object that inherits from .
- /// was added to support those cases where the caller is required in the host configuration.
- void ConfigureHost(Test hostTest);
- }
+ ///
+ /// Creates and configures the of this .
+ ///
+ /// The object that inherits from .
+ /// was added to support those cases where the caller is required in the host configuration.
+ void ConfigureHost(Test hostTest);
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/IHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting/IHostFixture.cs
index b97e197..92b247f 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/IHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/IHostFixture.cs
@@ -2,27 +2,26 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Provides a way to support app and lifetime management in unit tests.
+///
+///
+///
+///
+///
+public interface IHostFixture : IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable
{
///
- /// Provides a way to support app and lifetime management in unit tests.
+ /// Gets the initialized by either the or .
///
- ///
- ///
- ///
- ///
- public interface IHostFixture : IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable
- {
- ///
- /// Gets the initialized by either the or .
- ///
- /// The initialized by the or .
- IHost Host { get; }
+ /// The initialized by the or .
+ IHost Host { get; }
- ///
- /// Gets or sets the delegate that adds configuration and environment information to a .
- ///
- /// The delegate that adds configuration and environment information to a .
- Action ConfigureCallback { get; set; }
- }
+ ///
+ /// Gets or sets the delegate that adds configuration and environment information to a .
+ ///
+ /// The delegate that adds configuration and environment information to a .
+ Action ConfigureCallback { get; set; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/IHostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting/IHostTest.cs
index 0314a75..64e48f9 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/IHostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/IHostTest.cs
@@ -1,19 +1,18 @@
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Represents the members needed for bare-bone DI testing with support for .
+///
+///
+///
+///
+public interface IHostTest : IConfigurationTest, IEnvironmentTest, ITest
{
///
- /// Represents the members needed for bare-bone DI testing with support for .
+ /// Gets the initialized by the .
///
- ///
- ///
- ///
- public interface IHostTest : IConfigurationTest, IEnvironmentTest, ITest
- {
- ///
- /// Gets the initialized by the .
- ///
- /// The initialized by the .
- IHost Host { get; }
- }
+ /// The initialized by the .
+ IHost Host { get; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/IMinimalHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting/IMinimalHostFixture.cs
index bafe994..1c95f83 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/IMinimalHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/IMinimalHostFixture.cs
@@ -1,27 +1,26 @@
using Microsoft.Extensions.Hosting;
using System;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Provides a way to use Microsoft Dependency Injection in unit tests (minimal style).
+///
+///
+///
+///
+public interface IMinimalHostFixture : IHostFixture
{
///
- /// Provides a way to use Microsoft Dependency Injection in unit tests (minimal style).
+ /// Gets or sets the delegate that provides a way to override the defaults set up by .
///
- ///
- ///
- ///
- public interface IMinimalHostFixture : IHostFixture
- {
- ///
- /// Gets or sets the delegate that provides a way to override the defaults set up by .
- ///
- /// The delegate that provides a way to override the .
- Action ConfigureHostCallback { get; set; }
+ /// The delegate that provides a way to override the .
+ Action ConfigureHostCallback { get; set; }
- ///
- /// Creates and configures the of this .
- ///
- /// The object that inherits from .
- /// was added to support those cases where the caller is required in the host configuration.
- void ConfigureHost(Test hostTest);
- }
+ ///
+ /// Creates and configures the of this .
+ ///
+ /// The object that inherits from .
+ /// was added to support those cases where the caller is required in the host configuration.
+ void ConfigureHost(Test hostTest);
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/Internal/HostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting/Internal/HostTest.cs
index 6857372..87268ba 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/Internal/HostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/Internal/HostTest.cs
@@ -2,57 +2,56 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.Internal
+namespace Codebelt.Extensions.Xunit.Hosting.Internal;
+
+internal sealed class HostTest : HostTest
{
- internal sealed class HostTest : HostTest
- {
- private readonly Action _serviceConfigurator;
- private readonly Action _serviceConfiguratorWithContext;
- private readonly Action _hostConfigurator;
- private HostBuilderContext _hostBuilderContext;
+ private readonly Action _serviceConfigurator;
+ private readonly Action _serviceConfiguratorWithContext;
+ private readonly Action _hostConfigurator;
+ private HostBuilderContext _hostBuilderContext;
- internal HostTest(Action serviceConfigurator, Action hostConfigurator, IGenericHostFixture hostFixture) : base(true, hostFixture, callerType: serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
- {
- _serviceConfigurator = serviceConfigurator;
- _hostConfigurator = hostConfigurator;
- InitializeHostFixture(hostFixture);
- }
+ internal HostTest(Action serviceConfigurator, Action hostConfigurator, IGenericHostFixture hostFixture) : base(true, hostFixture, callerType: serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
+ {
+ _serviceConfigurator = serviceConfigurator;
+ _hostConfigurator = hostConfigurator;
+ InitializeHostFixture(hostFixture);
+ }
- internal HostTest(Action serviceConfigurator, Action hostConfigurator, IGenericHostFixture hostFixture) : base(true, hostFixture, callerType: serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
- {
- _serviceConfiguratorWithContext = serviceConfigurator;
- _hostConfigurator = hostConfigurator;
- InitializeHostFixture(hostFixture);
- }
+ internal HostTest(Action serviceConfigurator, Action hostConfigurator, IGenericHostFixture hostFixture) : base(true, hostFixture, callerType: serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
+ {
+ _serviceConfiguratorWithContext = serviceConfigurator;
+ _hostConfigurator = hostConfigurator;
+ InitializeHostFixture(hostFixture);
+ }
- private void InitializeHostFixture(IGenericHostFixture hostFixture)
+ private void InitializeHostFixture(IGenericHostFixture hostFixture)
+ {
+ if (!hostFixture.HasValidState())
{
- if (!hostFixture.HasValidState())
- {
- hostFixture.ConfigureHostCallback = ConfigureHost;
- hostFixture.ConfigureCallback = Configure;
- hostFixture.ConfigureServicesCallback = ConfigureServices;
- hostFixture.ConfigureHost(this);
- }
- Host = hostFixture.Host;
- Configure(hostFixture.Configuration, hostFixture.Environment);
+ hostFixture.ConfigureHostCallback = ConfigureHost;
+ hostFixture.ConfigureCallback = Configure;
+ hostFixture.ConfigureServicesCallback = ConfigureServices;
+ hostFixture.ConfigureHost(this);
}
+ Host = hostFixture.Host;
+ Configure(hostFixture.Configuration, hostFixture.Environment);
+ }
- protected override void ConfigureHost(IHostBuilder hb)
- {
- _hostBuilderContext = new HostBuilderContext(hb.Properties);
- _hostConfigurator?.Invoke(hb);
- }
+ protected override void ConfigureHost(IHostBuilder hb)
+ {
+ _hostBuilderContext = new HostBuilderContext(hb.Properties);
+ _hostConfigurator?.Invoke(hb);
+ }
- public override void ConfigureServices(IServiceCollection services)
+ public override void ConfigureServices(IServiceCollection services)
+ {
+ _serviceConfigurator?.Invoke(services);
+ _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
{
- _serviceConfigurator?.Invoke(services);
- _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
- {
- hbc.Configuration = Configuration;
- hbc.HostingEnvironment = Environment;
- return hbc;
- }), services);
- }
+ hbc.Configuration = Configuration;
+ hbc.HostingEnvironment = Environment;
+ return hbc;
+ }), services);
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/Internal/MinimalHostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting/Internal/MinimalHostTest.cs
index e6233b8..b920ed8 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/Internal/MinimalHostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/Internal/MinimalHostTest.cs
@@ -2,52 +2,51 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting.Internal
+namespace Codebelt.Extensions.Xunit.Hosting.Internal;
+
+internal sealed class MinimalHostTest : MinimalHostTest
{
- internal sealed class MinimalHostTest : MinimalHostTest
- {
- private readonly Action _serviceConfigurator;
- private readonly Action _serviceConfiguratorWithContext;
- private readonly Action _hostConfigurator;
- private HostBuilderContext _hostBuilderContext;
+ private readonly Action _serviceConfigurator;
+ private readonly Action _serviceConfiguratorWithContext;
+ private readonly Action _hostConfigurator;
+ private HostBuilderContext _hostBuilderContext;
- internal MinimalHostTest(Action serviceConfigurator, Action hostConfigurator, IMinimalHostFixture hostFixture) : base(true, hostFixture, callerType: serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
- {
- _serviceConfigurator = serviceConfigurator;
- _hostConfigurator = hostConfigurator;
- InitializeHostFixture(hostFixture);
- }
+ internal MinimalHostTest(Action serviceConfigurator, Action hostConfigurator, IMinimalHostFixture hostFixture) : base(true, hostFixture, callerType: serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
+ {
+ _serviceConfigurator = serviceConfigurator;
+ _hostConfigurator = hostConfigurator;
+ InitializeHostFixture(hostFixture);
+ }
- internal MinimalHostTest(Action serviceConfigurator, Action hostConfigurator, IMinimalHostFixture hostFixture) : base(true, hostFixture, callerType: serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
- {
- _serviceConfiguratorWithContext = serviceConfigurator;
- _hostConfigurator = hostConfigurator;
- InitializeHostFixture(hostFixture);
- }
+ internal MinimalHostTest(Action serviceConfigurator, Action hostConfigurator, IMinimalHostFixture hostFixture) : base(true, hostFixture, callerType: serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType())
+ {
+ _serviceConfiguratorWithContext = serviceConfigurator;
+ _hostConfigurator = hostConfigurator;
+ InitializeHostFixture(hostFixture);
+ }
- private void InitializeHostFixture(IMinimalHostFixture hostFixture)
+ private void InitializeHostFixture(IMinimalHostFixture hostFixture)
+ {
+ if (!hostFixture.HasValidState())
{
- if (!hostFixture.HasValidState())
- {
- hostFixture.ConfigureCallback = Configure;
- hostFixture.ConfigureHostCallback = ConfigureHost;
- hostFixture.ConfigureHost(this);
- }
- Host = hostFixture.Host;
- Configure(hostFixture.Configuration, hostFixture.Environment);
+ hostFixture.ConfigureCallback = Configure;
+ hostFixture.ConfigureHostCallback = ConfigureHost;
+ hostFixture.ConfigureHost(this);
}
+ Host = hostFixture.Host;
+ Configure(hostFixture.Configuration, hostFixture.Environment);
+ }
- protected override void ConfigureHost(IHostApplicationBuilder hb)
+ protected override void ConfigureHost(IHostApplicationBuilder hb)
+ {
+ _hostBuilderContext = new HostBuilderContext(hb.Properties);
+ _hostConfigurator?.Invoke(hb);
+ _serviceConfigurator?.Invoke(hb.Services);
+ _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
{
- _hostBuilderContext = new HostBuilderContext(hb.Properties);
- _hostConfigurator?.Invoke(hb);
- _serviceConfigurator?.Invoke(hb.Services);
- _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc =>
- {
- hbc.Configuration = hb.Configuration;
- hbc.HostingEnvironment = hb.Environment;
- return hbc;
- }), hb.Services);
- }
+ hbc.Configuration = hb.Configuration;
+ hbc.HostingEnvironment = hb.Environment;
+ return hbc;
+ }), hb.Services);
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs
index c85e32e..ac007f6 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs
@@ -1,75 +1,87 @@
-using System;
+using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Extension methods for the interface.
+///
+public static class LoggerExtensions
{
///
- /// Extension methods for the interface.
+ /// Returns the associated that is provided when settings up services from or related.
///
- public static class LoggerExtensions
+ /// The from which to retrieve the .
+ /// The category name for messages produced by the -or- null for messages produced by all loggers.
+ /// Returns an implementation of with all logged entries expressed as .
+ ///
+ /// cannot be null.
+ ///
+ ///
+ /// does not contain a test store.
+ ///
+ ///
+ /// does not contain a test store for the specified .
+ ///
+ public static ITestStore GetTestStore(this ILogger logger, string categoryName = null)
{
- ///
- /// Returns the associated that is provided when settings up services from or related.
- ///
- /// The from which to retrieve the .
- /// The category name for messages produced by the -or- null for messages produced by all loggers.
- /// Returns an implementation of with all logged entries expressed as .
- ///
- /// cannot be null.
- ///
- ///
- /// does not contain a test store.
- ///
- ///
- /// does not contain a test store for the specified .
- ///
- public static ITestStore GetTestStore(this ILogger logger, string categoryName = null)
+ if (logger == null) { throw new ArgumentNullException(nameof(logger)); }
+ var internalLogger = GetInternalLogger(logger);
+ if (internalLogger != null)
{
- if (logger == null) { throw new ArgumentNullException(nameof(logger)); }
- var loggerType = logger.GetType();
- var internalLogger = loggerType.GetRuntimeFields().SingleOrDefault(fi => fi.Name == "_logger")?.GetValue(logger);
- if (internalLogger != null)
- {
- var internalLoggerType = internalLogger.GetType();
- var internalLoggers = internalLoggerType.GetRuntimeProperties().SingleOrDefault(pi => pi.Name == "Loggers")?.GetValue(internalLogger);
- if (internalLoggers != null)
- {
- foreach (var loggerInformation in (IEnumerable)internalLoggers)
- {
- var loggerInformationType = loggerInformation.GetType();
- var providerType = loggerInformationType.GetProperty("ProviderType")?.GetValue(loggerInformation) as Type;
- if (providerType == typeof(XunitTestLoggerProvider))
- {
- var xunitTestLogger = loggerInformationType.GetProperty("Logger")?.GetValue(loggerInformation) as XunitTestLogger;
- if (xunitTestLogger == null) { continue; }
- return categoryName == null
- ? xunitTestLogger.Provider
- : xunitTestLogger.Provider[categoryName];
- }
- }
- }
- }
- throw new ArgumentException($"Logger does not contain a test store; did you remember to call {nameof(ServiceCollectionExtensions.AddXunitTestLogging)} before calling this method?", nameof(logger));
+ var store = FindTestStore(internalLogger, categoryName);
+ if (store != null) { return store; }
}
+ throw new ArgumentException($"Logger does not contain a test store; did you remember to call {nameof(ServiceCollectionExtensions.AddXunitTestLogging)} before calling this method?", nameof(logger));
+ }
+
+ ///
+ /// Returns the associated that is provided when settings up services from or related.
+ ///
+ /// The from which to retrieve the .
+ /// Returns an implementation of with all logged entries expressed as .
+ ///
+ /// cannot be null.
+ ///
+ ///
+ /// does not contain a test store.
+ ///
+ public static ITestStore GetTestStore(this ILogger logger)
+ {
+ return GetTestStore(logger, typeof(T).FullName);
+ }
+
+ private static object GetInternalLogger(ILogger logger)
+ {
+ var loggerType = logger.GetType();
+ return loggerType.GetRuntimeFields().SingleOrDefault(fi => fi.Name == "_logger")?.GetValue(logger);
+ }
- ///
- /// Returns the associated that is provided when settings up services from or related.
- ///
- /// The from which to retrieve the .
- /// Returns an implementation of with all logged entries expressed as .
- ///
- /// cannot be null.
- ///
- ///
- /// does not contain a test store.
- ///
- public static ITestStore GetTestStore(this ILogger logger)
+ private static ITestStore FindTestStore(object internalLogger, string categoryName)
+ {
+ var internalLoggerType = internalLogger.GetType();
+ var internalLoggers = internalLoggerType.GetRuntimeProperties().SingleOrDefault(pi => pi.Name == "Loggers")?.GetValue(internalLogger);
+ if (internalLoggers == null) { return null; }
+ foreach (var loggerInformation in (IEnumerable)internalLoggers)
{
- return GetTestStore(logger, typeof(T).FullName);
+ var store = TryGetTestStore(loggerInformation, categoryName);
+ if (store != null) { return store; }
}
+ return null;
+ }
+
+ private static ITestStore TryGetTestStore(object loggerInformation, string categoryName)
+ {
+ var loggerInformationType = loggerInformation.GetType();
+ var providerType = loggerInformationType.GetProperty("ProviderType")?.GetValue(loggerInformation) as Type;
+ if (providerType != typeof(XunitTestLoggerProvider)) { return null; }
+ if (loggerInformationType.GetProperty("Logger")?.GetValue(loggerInformation) is not XunitTestLogger xunitTestLogger) { return null; }
+ return categoryName == null
+ ? xunitTestLogger.Provider
+ : xunitTestLogger.Provider[categoryName];
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/ManagedHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting/ManagedHostFixture.cs
index 7a593c7..5aa7bd1 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/ManagedHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/ManagedHostFixture.cs
@@ -6,90 +6,89 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Provides a default implementation of the interface.
+///
+///
+///
+public class ManagedHostFixture : HostFixture, IGenericHostFixture
{
///
- /// Provides a default implementation of the interface.
+ /// Initializes a new instance of the class.
///
- ///
- ///
- public class ManagedHostFixture : HostFixture, IGenericHostFixture
+ public ManagedHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- public ManagedHostFixture()
- {
- }
+ }
- ///
- /// Creates and configures the of this instance.
- ///
- /// The object that inherits from .
- /// was added to support those cases where the caller is required in the host configuration.
- ///
- /// is null.
- ///
- ///
- /// is not assignable from .
- ///
- public virtual void ConfigureHost(Test hostTest)
- {
- if (hostTest == null) { throw new ArgumentNullException(nameof(hostTest)); }
- if (!HasTypes(hostTest.GetType(), typeof(HostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(HostTest<>), $"{nameof(hostTest)} is not assignable from HostTest."); }
- if (!this.HasValidState()) { return; } // had to include this due to dual-call this method (one uncontrolled from xUnit library reflection magic; second controlled from this library)
+ ///
+ /// Creates and configures the of this instance.
+ ///
+ /// The object that inherits from .
+ /// was added to support those cases where the caller is required in the host configuration.
+ ///
+ /// is null.
+ ///
+ ///
+ /// is not assignable from .
+ ///
+ public virtual void ConfigureHost(Test hostTest)
+ {
+ if (hostTest == null) { throw new ArgumentNullException(nameof(hostTest)); }
+ if (!HasTypes(hostTest.GetType(), typeof(HostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(HostTest<>), $"{nameof(hostTest)} is not assignable from HostTest."); }
+ if (!this.HasValidState()) { return; } // had to include this due to dual-call this method (one uncontrolled from xUnit library reflection magic; second controlled from this library)
- var hb = new HostBuilder()
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseEnvironment("Development")
- .ConfigureAppConfiguration((context, config) =>
- {
- config
- .AddJsonFile("appsettings.json", true, true)
- .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true, true)
- .AddEnvironmentVariables();
+ var hb = new HostBuilder()
+ .UseContentRoot(Directory.GetCurrentDirectory())
+ .UseEnvironment("Development")
+ .ConfigureAppConfiguration((context, config) =>
+ {
+ config
+ .AddJsonFile("appsettings.json", true, true)
+ .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true, true)
+ .AddEnvironmentVariables();
- ConfigureCallback(config.Build(), context.HostingEnvironment);
- })
- .ConfigureServices((context, services) =>
- {
- Configuration = context.Configuration;
- Environment = context.HostingEnvironment;
- ConfigureServicesCallback(services);
- })
- .ConfigureHostConfiguration(builder =>
+ ConfigureCallback(config.Build(), context.HostingEnvironment);
+ })
+ .ConfigureServices((context, services) =>
+ {
+ Configuration = context.Configuration;
+ Environment = context.HostingEnvironment;
+ ConfigureServicesCallback(services);
+ })
+ .ConfigureHostConfiguration(builder =>
+ {
+ builder.AddInMemoryCollection(new Dictionary
{
- builder.AddInMemoryCollection(new Dictionary
- {
- { HostDefaults.ApplicationKey, hostTest.CallerType.Assembly.GetName().Name }
- });
+ { HostDefaults.ApplicationKey, hostTest.CallerType.Assembly.GetName().Name }
});
+ });
#if NET9_0_OR_GREATER
- hb.UseDefaultServiceProvider(o =>
- {
- o.ValidateOnBuild = true;
- o.ValidateScopes = true;
- });
+ hb.UseDefaultServiceProvider(o =>
+ {
+ o.ValidateOnBuild = true;
+ o.ValidateScopes = true;
+ });
#endif
- ConfigureHostCallback(hb);
+ ConfigureHostCallback(hb);
- Host = hb.Build();
+ Host = hb.Build();
- AsyncHostRunnerCallback(Host, CancellationToken.None);
- }
+ AsyncHostRunnerCallback(Host, CancellationToken.None);
+ }
- ///
- /// Gets or sets the delegate that initializes the host builder.
- ///
- /// The delegate that initializes the host builder.
- public Action ConfigureHostCallback { get; set; }
+ ///
+ /// Gets or sets the delegate that initializes the host builder.
+ ///
+ /// The delegate that initializes the host builder.
+ public Action ConfigureHostCallback { get; set; }
- ///
- /// Gets or sets the delegate that adds services to the container.
- ///
- /// The delegate that adds services to the container.
- public Action ConfigureServicesCallback { get; set; }
- }
+ ///
+ /// Gets or sets the delegate that adds services to the container.
+ ///
+ /// The delegate that adds services to the container.
+ public Action ConfigureServicesCallback { get; set; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/ManagedMinimalHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting/ManagedMinimalHostFixture.cs
index bf01d9a..b6319d2 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/ManagedMinimalHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/ManagedMinimalHostFixture.cs
@@ -2,61 +2,60 @@
using System.Threading;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Provides a default implementation of the interface.
+///
+///
+///
+/// This is the "modern" minimal style implementation of .
+public class ManagedMinimalHostFixture : HostFixture, IMinimalHostFixture
{
///
- /// Provides a default implementation of the interface.
+ /// Initializes a new instance of the class.
///
- ///
- ///
- /// This is the "modern" minimal style implementation of .
- public class ManagedMinimalHostFixture : HostFixture, IMinimalHostFixture
+ public ManagedMinimalHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- public ManagedMinimalHostFixture()
- {
- }
-
- ///
- /// Creates and configures the of this instance.
- ///
- /// The object that inherits from .
- /// was added to support those cases where the caller is required in the host configuration.
- ///
- /// is null.
- ///
- ///
- /// is not assignable from .
- ///
- public virtual void ConfigureHost(Test hostTest)
- {
- if (hostTest == null) { throw new ArgumentNullException(nameof(hostTest)); }
- if (!HasTypes(hostTest.GetType(), typeof(MinimalHostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(MinimalHostTest<>), $"{nameof(hostTest)} is not assignable from MinimalHostTest."); }
+ }
- var hb = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(new HostApplicationBuilderSettings()
- {
- EnvironmentName = "Development",
- ApplicationName = hostTest.CallerType.Assembly.GetName().Name,
- });
+ ///
+ /// Creates and configures the of this instance.
+ ///
+ /// The object that inherits from .
+ /// was added to support those cases where the caller is required in the host configuration.
+ ///
+ /// is null.
+ ///
+ ///
+ /// is not assignable from .
+ ///
+ public virtual void ConfigureHost(Test hostTest)
+ {
+ if (hostTest == null) { throw new ArgumentNullException(nameof(hostTest)); }
+ if (!HasTypes(hostTest.GetType(), typeof(MinimalHostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(MinimalHostTest<>), $"{nameof(hostTest)} is not assignable from MinimalHostTest."); }
- Configuration = hb.Configuration;
- Environment = hb.Environment;
+ var hb = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(new HostApplicationBuilderSettings()
+ {
+ EnvironmentName = "Development",
+ ApplicationName = hostTest.CallerType.Assembly.GetName().Name,
+ });
- ConfigureCallback(Configuration, Environment);
+ Configuration = hb.Configuration;
+ Environment = hb.Environment;
- ConfigureHostCallback(hb);
+ ConfigureCallback(Configuration, Environment);
- Host = hb.Build();
+ ConfigureHostCallback(hb);
- AsyncHostRunnerCallback(Host, CancellationToken.None);
- }
+ Host = hb.Build();
- ///
- /// Gets or sets the delegate that initializes the host application builder.
- ///
- /// The delegate that initializes the host application builder.
- public Action ConfigureHostCallback { get; set; }
+ AsyncHostRunnerCallback(Host, CancellationToken.None);
}
+
+ ///
+ /// Gets or sets the delegate that initializes the host application builder.
+ ///
+ /// The delegate that initializes the host application builder.
+ public Action ConfigureHostCallback { get; set; }
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostFixtureExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostFixtureExtensions.cs
index c9a1543..b27a293 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostFixtureExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostFixtureExtensions.cs
@@ -1,24 +1,23 @@
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Extension methods for the interface.
+///
+public static class MinimalHostFixtureExtensions
{
///
- /// Extension methods for the interface.
+ /// Determines whether the specified has a valid state.
///
- public static class MinimalHostFixtureExtensions
+ /// The to check.
+ /// true if the specified has a valid state; otherwise, false .
+ ///
+ /// A valid state is defined as having non-null values for the following properties:
+ /// , and .
+ ///
+ public static bool HasValidState(this IMinimalHostFixture hostFixture)
{
- ///
- /// Determines whether the specified has a valid state.
- ///
- /// The to check.
- /// true if the specified has a valid state; otherwise, false .
- ///
- /// A valid state is defined as having non-null values for the following properties:
- /// , and .
- ///
- public static bool HasValidState(this IMinimalHostFixture hostFixture)
- {
- return hostFixture.Host != null &&
- hostFixture.ConfigureHostCallback != null &&
- hostFixture.ConfigureCallback != null;
- }
+ return hostFixture.Host != null &&
+ hostFixture.ConfigureHostCallback != null &&
+ hostFixture.ConfigureCallback != null;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTest.cs b/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTest.cs
index bc76807..6a4d73b 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTest.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTest.cs
@@ -1,78 +1,81 @@
-using System;
+using System;
using Microsoft.Extensions.Hosting;
using Xunit;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Represents the non-generic base class from where its generic equivalent should derive.
+///
+///
+public abstract class MinimalHostTest : HostTest
{
///
- /// Represents the non-generic base class from where its generic equivalent should derive.
+ /// Initializes a new instance of the class.
///
- ///
- public abstract class MinimalHostTest : HostTest
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ protected MinimalHostTest(ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
+ {
+ }
+
+
+ ///
+ /// Provides a way to override the defaults.
+ ///
+ /// The that initializes an instance of .
+ protected virtual void ConfigureHost(IHostApplicationBuilder hb)
{
- ///
- /// Initializes a new instance of the class.
- ///
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- protected MinimalHostTest(ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
- {
- }
-
-
- ///
- /// Provides a way to override the defaults.
- ///
- /// The that initializes an instance of .
- protected virtual void ConfigureHost(IHostApplicationBuilder hb)
- {
- }
}
+}
+///
+/// Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection (minimal style), should derive.
+///
+/// The type of the object that implements the interface.
+///
+///
+/// The class needed to be designed in this rather complex way, as this is the only way that xUnit supports a shared context. The need for shared context is theoretical at best, but it does opt-in for Scoped instances.
+public abstract class MinimalHostTest : MinimalHostTest, IClassFixture where T : class, IMinimalHostFixture
+{
///
- /// Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection (minimal style), should derive.
+ /// Initializes a new instance of the class.
///
- /// The type of the object that implements the interface.
- ///
- ///
- /// The class needed to be designed in this rather complex way, as this is the only way that xUnit supports a shared context. The need for shared context is theoretical at best, but it does opt-in for Scoped instances.
- public abstract class MinimalHostTest : MinimalHostTest, IClassFixture where T : class, IMinimalHostFixture
+ /// An implementation of the interface.
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ ///
+ /// is null.
+ ///
+ protected MinimalHostTest(T hostFixture, ITestOutputHelper output = null, Type callerType = null) : this(false, hostFixture, output, callerType)
{
- ///
- /// Initializes a new instance of the class.
- ///
- /// An implementation of the interface.
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- ///
- /// is null.
- ///
- protected MinimalHostTest(T hostFixture, ITestOutputHelper output = null, Type callerType = null) : this(false, hostFixture, output, callerType)
- {
- }
+ }
- ///
- /// Initializes a new instance of the class.
- ///
- /// A value indicating whether to skip the host fixture initialization.
- /// An implementation of the interface.
- /// An implementation of the interface.
- /// The of caller that ends up invoking this instance.
- ///
- /// is null.
- ///
- protected MinimalHostTest(bool skipHostFixtureInitialization, T hostFixture, ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// A value indicating whether to skip the host fixture initialization.
+ /// An implementation of the interface.
+ /// An implementation of the interface.
+ /// The of caller that ends up invoking this instance.
+ ///
+ /// is null.
+ ///
+ protected MinimalHostTest(bool skipHostFixtureInitialization, T hostFixture, ITestOutputHelper output = null, Type callerType = null) : base(output, callerType)
+ {
+#if NETSTANDARD2_0
+ if (hostFixture == null) { throw new ArgumentNullException(nameof(hostFixture)); }
+#else
+ ArgumentNullException.ThrowIfNull(hostFixture);
+#endif
+ if (skipHostFixtureInitialization) { return; }
+ if (!hostFixture.HasValidState())
{
- if (hostFixture == null) { throw new ArgumentNullException(nameof(hostFixture)); }
- if (skipHostFixtureInitialization) { return; }
- if (!hostFixture.HasValidState())
- {
- hostFixture.ConfigureCallback = Configure;
- hostFixture.ConfigureHostCallback = ConfigureHost;
- hostFixture.ConfigureHost(this);
- }
- Host = hostFixture.Host;
- Configure(hostFixture.Configuration, hostFixture.Environment);
+ hostFixture.ConfigureCallback = Configure;
+ hostFixture.ConfigureHostCallback = ConfigureHost;
+ hostFixture.ConfigureHost(this);
}
+ Host = hostFixture.Host;
+ Configure(hostFixture.Configuration, hostFixture.Environment);
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTestFactory.cs b/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTestFactory.cs
index 340993a..b81903a 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTestFactory.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTestFactory.cs
@@ -2,35 +2,34 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Provides a set of static methods for unit testing (minimal style).
+///
+public static class MinimalHostTestFactory
{
///
- /// Provides a set of static methods for unit testing (minimal style).
+ /// Creates and returns an implementation.
///
- public static class MinimalHostTestFactory
+ /// The which may be configured.
+ /// The which may be configured.
+ /// An optional implementation to use instead of the default instance.
+ /// An instance of an implementation.
+ public static IHostTest Create(Action serviceSetup = null, Action hostSetup = null, IMinimalHostFixture hostFixture = null)
{
- ///
- /// Creates and returns an implementation.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// An optional implementation to use instead of the default instance.
- /// An instance of an implementation.
- public static IHostTest Create(Action serviceSetup = null, Action hostSetup = null, IMinimalHostFixture hostFixture = null)
- {
- return new Internal.MinimalHostTest(serviceSetup, hostSetup, hostFixture ?? new ManagedMinimalHostFixture());
- }
+ return new Internal.MinimalHostTest(serviceSetup, hostSetup, hostFixture ?? new ManagedMinimalHostFixture());
+ }
- ///
- /// Creates and returns an implementation.
- ///
- /// The which may be configured.
- /// The which may be configured.
- /// An optional implementation to use instead of the default instance.
- /// An instance of an implementation.
- public static IHostTest CreateWithHostBuilderContext(Action serviceSetup = null, Action hostSetup = null, IMinimalHostFixture hostFixture = null)
- {
- return new Internal.MinimalHostTest(serviceSetup, hostSetup, hostFixture ?? new ManagedMinimalHostFixture());
- }
+ ///
+ /// Creates and returns an implementation.
+ ///
+ /// The which may be configured.
+ /// The which may be configured.
+ /// An optional implementation to use instead of the default instance.
+ /// An instance of an implementation.
+ public static IHostTest CreateWithHostBuilderContext(Action serviceSetup = null, Action hostSetup = null, IMinimalHostFixture hostFixture = null)
+ {
+ return new Internal.MinimalHostTest(serviceSetup, hostSetup, hostFixture ?? new ManagedMinimalHostFixture());
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/SelfManagedHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting/SelfManagedHostFixture.cs
index d68cbdf..17e31bd 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/SelfManagedHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/SelfManagedHostFixture.cs
@@ -1,21 +1,20 @@
using System.Threading.Tasks;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Represents a self-managed implementation of the class.
+///
+public sealed class SelfManagedHostFixture : ManagedHostFixture
{
///
- /// Represents a self-managed implementation of the class.
+ /// Initializes a new instance of the class.
///
- public sealed class SelfManagedHostFixture : ManagedHostFixture
+ ///
+ /// This constructor sets the to a no-op asynchronous delegate.
+ ///
+ public SelfManagedHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- ///
- /// This constructor sets the to a no-op asynchronous delegate.
- ///
- public SelfManagedHostFixture()
- {
- AsyncHostRunnerCallback = (_, __) => Task.CompletedTask;
- }
+ AsyncHostRunnerCallback = (_, __) => Task.CompletedTask;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/SelfManagedMinimalHostFixture.cs b/src/Codebelt.Extensions.Xunit.Hosting/SelfManagedMinimalHostFixture.cs
index 8090797..ebe8a30 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/SelfManagedMinimalHostFixture.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/SelfManagedMinimalHostFixture.cs
@@ -1,21 +1,20 @@
using System.Threading.Tasks;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Represents a self-managed implementation of the class.
+///
+public sealed class SelfManagedMinimalHostFixture : ManagedMinimalHostFixture
{
///
- /// Represents a self-managed implementation of the class.
+ /// Initializes a new instance of the class.
///
- public sealed class SelfManagedMinimalHostFixture : ManagedMinimalHostFixture
+ ///
+ /// This constructor sets the to a no-op asynchronous delegate.
+ ///
+ public SelfManagedMinimalHostFixture()
{
- ///
- /// Initializes a new instance of the class.
- ///
- ///
- /// This constructor sets the to a no-op asynchronous delegate.
- ///
- public SelfManagedMinimalHostFixture()
- {
- AsyncHostRunnerCallback = (_, __) => Task.CompletedTask;
- }
+ AsyncHostRunnerCallback = (_, __) => Task.CompletedTask;
}
}
diff --git a/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs
index 727897c..8967546 100644
--- a/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs
+++ b/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs
@@ -1,111 +1,115 @@
-using System;
+using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
-namespace Codebelt.Extensions.Xunit.Hosting
+namespace Codebelt.Extensions.Xunit.Hosting;
+
+///
+/// Extension methods for the interface.
+///
+public static class ServiceCollectionExtensions
{
- ///
- /// Extension methods for the interface.
- ///
- public static class ServiceCollectionExtensions
+ ///
+ /// Adds a unit test optimized implementation of logging to the collection.
+ ///
+ /// The to extend.
+ /// The that specifies the minimum level to include for the logging.
+ /// A reference to so that additional configuration calls can be chained.
+ ///
+ /// cannot be null.
+ ///
+ public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, LogLevel minimumLevel = LogLevel.Trace)
{
- ///
- /// Adds a unit test optimized implementation of logging to the collection.
- ///
- /// The to extend.
- /// The that specifies the minimum level to include for the logging.
- /// A reference to so that additional configuration calls can be chained.
- ///
- /// cannot be null.
- ///
- public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, LogLevel minimumLevel = LogLevel.Trace)
+ if (services == null) { throw new ArgumentNullException(nameof(services)); }
+ if (services.Any(sd => sd.ServiceType == typeof(ITestOutputHelperAccessor)))
{
- if (services == null) { throw new ArgumentNullException(nameof(services)); }
- if (services.Any(sd => sd.ServiceType == typeof(ITestOutputHelperAccessor)))
- {
- AddTestOutputHelperAccessor(services, minimumLevel);
- }
- else
- {
- services.AddLogging(builder =>
- {
- builder.SetMinimumLevel(minimumLevel);
- builder.AddProvider(new XunitTestLoggerProvider());
- });
- }
- return services;
+ AddTestOutputHelperAccessor(services, minimumLevel);
}
-
- ///
- /// Adds a unit test optimized implementation of output logging to the collection.
- ///
- /// The to extend.
- /// The that provides the output for the logging.
- /// The that specifies the minimum level to include for the logging.
- /// A reference to so that additional configuration calls can be chained.
- ///
- /// cannot be null -or-
- /// cannot be null.
- ///
- public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, ITestOutputHelper output, LogLevel minimumLevel = LogLevel.Trace)
+ else
{
- if (services == null) { throw new ArgumentNullException(nameof(services)); }
- if (output == null) { throw new ArgumentNullException(nameof(output)); }
- if (services.Any(sd => sd.ServiceType == typeof(ITestOutputHelperAccessor)))
- {
- AddTestOutputHelperAccessor(services, minimumLevel);
- }
- else
+ services.AddLogging(builder =>
{
- services.AddLogging(builder =>
- {
- builder.SetMinimumLevel(minimumLevel);
- builder.AddProvider(new XunitTestLoggerProvider(output));
- });
- }
- return services;
+ builder.SetMinimumLevel(minimumLevel);
+ builder.AddProvider(new XunitTestLoggerProvider());
+ });
}
+ return services;
+ }
- private static void AddTestOutputHelperAccessor(IServiceCollection services, LogLevel minimumLevel)
+ ///
+ /// Adds a unit test optimized implementation of output logging to the collection.
+ ///
+ /// The to extend.
+ /// The that provides the output for the logging.
+ /// The that specifies the minimum level to include for the logging.
+ /// A reference to so that additional configuration calls can be chained.
+ ///
+ /// cannot be null -or-
+ /// cannot be null.
+ ///
+ public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, ITestOutputHelper output, LogLevel minimumLevel = LogLevel.Trace)
+ {
+#if NETSTANDARD2_0
+ if (services == null) { throw new ArgumentNullException(nameof(services)); }
+ if (output == null) { throw new ArgumentNullException(nameof(output)); }
+#else
+ ArgumentNullException.ThrowIfNull(services);
+ ArgumentNullException.ThrowIfNull(output);
+#endif
+ if (services.Any(sd => sd.ServiceType == typeof(ITestOutputHelperAccessor)))
+ {
+ AddTestOutputHelperAccessor(services, minimumLevel);
+ }
+ else
{
services.AddLogging(builder =>
{
builder.SetMinimumLevel(minimumLevel);
- builder.Services.AddSingleton(provider =>
- {
- var accessor = provider.GetRequiredService();
- return new XunitTestLoggerProvider(accessor);
- });
+ builder.AddProvider(new XunitTestLoggerProvider(output));
});
}
+ return services;
+ }
- ///
- /// Adds a default implementation of to the collection.
- ///
- /// The to extend.
- /// A reference to so that additional configuration calls can be chained.
- public static IServiceCollection AddXunitTestLoggingOutputHelperAccessor(this IServiceCollection services)
+ private static void AddTestOutputHelperAccessor(IServiceCollection services, LogLevel minimumLevel)
+ {
+ services.AddLogging(builder =>
{
- services.AddXunitTestLoggingOutputHelperAccessor();
- return services;
- }
+ builder.SetMinimumLevel(minimumLevel);
+ builder.Services.AddSingleton(provider =>
+ {
+ var accessor = provider.GetRequiredService();
+ return new XunitTestLoggerProvider(accessor);
+ });
+ });
+ }
- ///
- /// Adds a specified implementation of