diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42976aa..3e679f4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,10 +35,10 @@ jobs: 8.0.x 9.0.x 10.0.x - - name: Build - run: dotnet build src --configuration Release - - name: Tests - run: dotnet test src --configuration Release --no-build + - name: Build + run: dotnet build src/ServiceComposer.AspNetCore.Testing.sln --configuration Release + - name: Tests + run: dotnet test src/ServiceComposer.AspNetCore.Testing.sln --configuration Release --no-build - name: Upload packages if: matrix.name == 'Linux' uses: actions/upload-artifact@v6.0.0 diff --git a/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.Public_api_is_approved.verified.txt b/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.Public_api_is_approved.verified.txt new file mode 100644 index 0000000..992ad5c --- /dev/null +++ b/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.Public_api_is_approved.verified.txt @@ -0,0 +1,30 @@ +[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/ServiceComposer/ServiceComposer.AspNetCore.Testing")] +[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v10.0", FrameworkDisplayName=".NET 10.0")] +namespace ServiceComposer.AspNetCore.Testing +{ + public class SelfContainedWebApplicationFactoryWithHost : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory + where TEntryPoint : class + { + public SelfContainedWebApplicationFactoryWithHost(System.Action configureServices, System.Action configure, string[] args = null) { } + public System.Action HostBuilderCustomization { get; set; } + public System.Action WebHostBuilderCustomization { get; set; } + protected override Microsoft.Extensions.Hosting.IHostBuilder CreateHostBuilder() { } + } + public class SelfContainedWebApplicationFactoryWithWebHost : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory + where TEntryPoint : class + { + public SelfContainedWebApplicationFactoryWithWebHost(System.Action configureServices, System.Action configure) { } + public SelfContainedWebApplicationFactoryWithWebHost(System.Action configureServices, System.Action configure) { } + public System.Action BuilderCustomization { get; set; } + protected override void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder) { } + protected override Microsoft.AspNetCore.Hosting.IWebHostBuilder CreateWebHostBuilder() { } + } + public class WebApplicationFactoryWithWebHost : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory + where TStartup : class + { + public WebApplicationFactoryWithWebHost() { } + public System.Action BuilderCustomization { get; set; } + protected override void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder) { } + protected override Microsoft.AspNetCore.Hosting.IWebHostBuilder CreateWebHostBuilder() { } + } +} \ No newline at end of file diff --git a/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.cs b/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.cs new file mode 100644 index 0000000..027da55 --- /dev/null +++ b/src/ServiceComposer.AspNetCore.Testing.Tests/PublicApiApprovalTests.cs @@ -0,0 +1,14 @@ +using PublicApiGenerator; +using VerifyXunit; + +namespace ServiceComposer.AspNetCore.Testing.Tests; + +public class PublicApiApprovalTests +{ + [Fact] + public Task Public_api_is_approved() + { + var publicApi = ApiGenerator.GeneratePublicApi(typeof(WebApplicationFactoryWithWebHost<>).Assembly); + return Verifier.Verify(publicApi); + } +} diff --git a/src/ServiceComposer.AspNetCore.Testing.Tests/SelfContainedWebApplicationFactoryTests.cs b/src/ServiceComposer.AspNetCore.Testing.Tests/SelfContainedWebApplicationFactoryTests.cs new file mode 100644 index 0000000..c4ab1ce --- /dev/null +++ b/src/ServiceComposer.AspNetCore.Testing.Tests/SelfContainedWebApplicationFactoryTests.cs @@ -0,0 +1,76 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace ServiceComposer.AspNetCore.Testing.Tests; + +public class SelfContainedWebApplicationFactoryTests +{ + [Fact] + public async Task SelfContainedFactoryWithHost_serves_request() + { + await using var factory = new SelfContainedWebApplicationFactoryWithHost( + services => services.AddSingleton(new Message("host")), + app => app.Run(async context => + { + var message = context.RequestServices.GetRequiredService(); + await context.Response.WriteAsync(message.Value); + })); + + var client = factory.CreateClient(); + var response = await client.GetStringAsync("/"); + + Assert.Equal("host", response); + } + + [Fact] + public async Task SelfContainedFactoryWithWebHost_invokes_builder_customization() + { + var builderCustomizationCalled = false; + await using var factory = new SelfContainedWebApplicationFactoryWithWebHost( + services => services.AddSingleton(new Message("webhost")), + app => app.Run(async context => + { + var message = context.RequestServices.GetRequiredService(); + await context.Response.WriteAsync(message.Value); + })) + { + BuilderCustomization = _ => builderCustomizationCalled = true + }; + + var client = factory.CreateClient(); + var response = await client.GetStringAsync("/"); + + Assert.Equal("webhost", response); + Assert.True(builderCustomizationCalled); + } + + [Fact] + public async Task WebApplicationFactoryWithWebHost_invokes_builder_customization() + { + var builderCustomizationCalled = false; + await using var factory = new WebApplicationFactoryWithWebHost + { + BuilderCustomization = _ => builderCustomizationCalled = true + }; + + var client = factory.CreateClient(); + var response = await client.GetStringAsync("/"); + + Assert.Equal("startup", response); + Assert.True(builderCustomizationCalled); + } + + class TestEntryPoint; + + class Startup + { + public void Configure(IApplicationBuilder app) + { + app.Run(context => context.Response.WriteAsync("startup")); + } + } + + record Message(string Value); +} diff --git a/src/ServiceComposer.AspNetCore.Testing.Tests/ServiceComposer.AspNetCore.Testing.Tests.csproj b/src/ServiceComposer.AspNetCore.Testing.Tests/ServiceComposer.AspNetCore.Testing.Tests.csproj new file mode 100644 index 0000000..367e041 --- /dev/null +++ b/src/ServiceComposer.AspNetCore.Testing.Tests/ServiceComposer.AspNetCore.Testing.Tests.csproj @@ -0,0 +1,27 @@ + + + + net10.0 + enable + enable + false + + + + + + + + + + + + + + + + + + + + diff --git a/src/ServiceComposer.AspNetCore.Testing.sln b/src/ServiceComposer.AspNetCore.Testing.sln index a7867d2..32a1cab 100644 --- a/src/ServiceComposer.AspNetCore.Testing.sln +++ b/src/ServiceComposer.AspNetCore.Testing.sln @@ -4,6 +4,8 @@ VisualStudioVersion = 15.0.26730.16 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceComposer.AspNetCore.Testing", "ServiceComposer.AspNetCore.Testing\ServiceComposer.AspNetCore.Testing.csproj", "{37843543-9921-4A2E-A62E-EC64E08A8BA6}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceComposer.AspNetCore.Testing.Tests", "ServiceComposer.AspNetCore.Testing.Tests\ServiceComposer.AspNetCore.Testing.Tests.csproj", "{83B393CD-C479-4306-B2B5-56EB3B75170E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -14,6 +16,10 @@ Global {37843543-9921-4A2E-A62E-EC64E08A8BA6}.Debug|Any CPU.Build.0 = Debug|Any CPU {37843543-9921-4A2E-A62E-EC64E08A8BA6}.Release|Any CPU.ActiveCfg = Release|Any CPU {37843543-9921-4A2E-A62E-EC64E08A8BA6}.Release|Any CPU.Build.0 = Release|Any CPU + {83B393CD-C479-4306-B2B5-56EB3B75170E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83B393CD-C479-4306-B2B5-56EB3B75170E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83B393CD-C479-4306-B2B5-56EB3B75170E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83B393CD-C479-4306-B2B5-56EB3B75170E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE