From 35befaac778e15d1ffd9dbaf6a233986b13ee832 Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Fri, 27 Sep 2024 14:40:00 +0200 Subject: [PATCH] Improve FilterCreationTests to catch changes better --- .../FilterCreationTests.cs | 67 +++++++------------ 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/tests/Tingle.AspNetCore.Swagger.Tests/FilterCreationTests.cs b/tests/Tingle.AspNetCore.Swagger.Tests/FilterCreationTests.cs index 7bb315b..fce9470 100644 --- a/tests/Tingle.AspNetCore.Swagger.Tests/FilterCreationTests.cs +++ b/tests/Tingle.AspNetCore.Swagger.Tests/FilterCreationTests.cs @@ -9,66 +9,45 @@ namespace Tingle.AspNetCore.Swagger.Tests; public class FilterCreationTests { [Fact] - public void InheritDocSchemaFilter_IsAdded() + public void InheritDocSchemaFilter_IsAdded_And_Created() { - var services = new ServiceCollection().AddLogging() - .AddSwaggerGen(o => o.IncludeXmlCommentsFromInheritDocs()) - .BuildServiceProvider(); + var services = CreateServiceProvider(); - var options = services.GetRequiredService>().Value; - var descriptors = options.SchemaFilterDescriptors.Where(sfd => sfd.Type == typeof(InheritDocSchemaFilter)); + var options1 = services.GetRequiredService>().Value; + var descriptors = options1.SchemaFilterDescriptors.Where(sfd => sfd.Type == typeof(InheritDocSchemaFilter)); var descriptor = Assert.Single(descriptors); var arguments = descriptor.Arguments; Assert.Equal(2, arguments.Length); Assert.IsType(arguments[0]); Assert.IsType(arguments[1]); - } - [Fact] - public void InheritDocSchemaFilter_CanBe_Created() - { - var services = new ServiceCollection().AddLogging() - .AddSwaggerGen(o => o.IncludeXmlCommentsFromInheritDocs()) - .BuildServiceProvider(); - - var options = services.GetRequiredService>().Value; - Assert.Single(options.SchemaFilters.OfType()); + var options2 = services.GetRequiredService>().Value; + Assert.Single(options2.SchemaFilters.OfType()); } [Fact] - public void EnumDescriptionsFilters_AreAdded() + public void EnumDescriptionsFilters_AreAdded_And_Created() { - var env = new FakeWebHostEnvironment { ApplicationName = "Test", ContentRootPath = Environment.CurrentDirectory, }; - var services = new ServiceCollection().AddLogging() - .AddSwaggerGen(o => o.IncludeXmlComments(true)) - .AddSwaggerEnumDescriptions() - .BuildServiceProvider(); - - var options = services.GetRequiredService>().Value; + var services = CreateServiceProvider(); - Assert.Single(options.SchemaFilterDescriptors, d => d.Type == typeof(EnumDescriptionsSchemaFilter)); - } - - [Fact] - public void EnumDescriptionsFilter_CanBe_Created() - { - var env = new FakeWebHostEnvironment { ApplicationName = "Test", ContentRootPath = Environment.CurrentDirectory, }; - var services = new ServiceCollection().AddLogging() - .AddSwaggerGen(o => o.IncludeXmlComments(true)) - .AddSwaggerEnumDescriptions() - .BuildServiceProvider(); + var options1 = services.GetRequiredService>().Value; + Assert.Single(options1.SchemaFilterDescriptors, d => d.Type == typeof(EnumDescriptionsSchemaFilter)); - var options = services.GetRequiredService>().Value; - Assert.Single(options.SchemaFilters.OfType()); + var options2 = services.GetRequiredService>().Value; + Assert.Single(options2.SchemaFilters.OfType()); } - private class FakeWebHostEnvironment : IWebHostEnvironment + private static IServiceProvider CreateServiceProvider(Action? configure = null) { - public string WebRootPath { get; set; } = default!; - public IFileProvider WebRootFileProvider { get; set; } = default!; - public string ApplicationName { get; set; } = default!; - public IFileProvider ContentRootFileProvider { get; set; } = default!; - public string ContentRootPath { get; set; } = default!; - public string EnvironmentName { get; set; } = default!; + var services = new ServiceCollection(); + services.AddLogging(); + services.AddSwaggerGen(options => + { + options.IncludeXmlComments(true); + options.IncludeXmlCommentsFromInheritDocs(); + }); + services.AddSwaggerEnumDescriptions(); + configure?.Invoke(services); + return services.BuildServiceProvider(); } }