Skip to content

Commit 344a49f

Browse files
committed
[Dto]: add partial hooks to generated CRUD tests for factory/client configuration
Generated CRUD test classes are now partial with two static partial methods: - ConfigureWebHost(IWebHostBuilder) — configure DI, database, auth - ConfigureClient(HttpClient) — add headers, tokens, base URL User creates a partial class in a separate file to implement either hook. If not implemented, defaults to no-op (standard partial method behavior). XML doc on the generated class shows usage example.
1 parent d5a904a commit 344a49f

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

packages/ZibStack.NET.Dto/src/ZibStack.NET.Dto/DtoGenerator.CrudGeneration.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,18 +876,44 @@ private static string GenerateCrudTestSource(CrudTestInfo info)
876876
sb.AppendLine("using System.Net;");
877877
sb.AppendLine("using System.Net.Http.Json;");
878878
sb.AppendLine("using Bogus;");
879+
sb.AppendLine("using Microsoft.AspNetCore.Hosting;");
879880
sb.AppendLine("using Microsoft.AspNetCore.Mvc.Testing;");
880881
sb.AppendLine("using Xunit;");
881882
sb.AppendLine();
882883
sb.AppendLine($"namespace {ns};");
883884
sb.AppendLine();
884-
sb.AppendLine($"public class {entity}CrudTests : IClassFixture<WebApplicationFactory<Program>>");
885+
sb.AppendLine($"/// <summary>");
886+
sb.AppendLine($"/// Auto-generated CRUD tests for <c>{entity}</c>.");
887+
sb.AppendLine($"/// To customize, create a partial class in a separate file:");
888+
sb.AppendLine($"/// <code>");
889+
sb.AppendLine($"/// public partial class {entity}CrudTests");
890+
sb.AppendLine($"/// {{");
891+
sb.AppendLine($"/// static partial void ConfigureWebHost(IWebHostBuilder builder)");
892+
sb.AppendLine($"/// => builder.ConfigureServices(s => s.AddDbContext&lt;...&gt;(...));");
893+
sb.AppendLine($"/// static partial void ConfigureClient(HttpClient client)");
894+
sb.AppendLine($"/// => client.DefaultRequestHeaders.Authorization = new(\"Bearer\", \"test-token\");");
895+
sb.AppendLine($"/// }}");
896+
sb.AppendLine($"/// </code>");
897+
sb.AppendLine($"/// </summary>");
898+
sb.AppendLine($"public partial class {entity}CrudTests : IClassFixture<WebApplicationFactory<Program>>");
885899
sb.AppendLine("{");
886900
sb.AppendLine(" private readonly HttpClient _client;");
887901
sb.AppendLine(" private readonly Faker _faker = new(\"en\");");
888902
sb.AppendLine();
889903
sb.AppendLine($" public {entity}CrudTests(WebApplicationFactory<Program> factory)");
890-
sb.AppendLine(" => _client = factory.CreateClient();");
904+
sb.AppendLine(" {");
905+
sb.AppendLine(" var f = factory.WithWebHostBuilder(builder =>");
906+
sb.AppendLine(" {");
907+
sb.AppendLine(" ConfigureWebHost(builder);");
908+
sb.AppendLine(" });");
909+
sb.AppendLine(" _client = f.CreateClient();");
910+
sb.AppendLine(" ConfigureClient(_client);");
911+
sb.AppendLine(" }");
912+
sb.AppendLine();
913+
sb.AppendLine(" /// <summary>Override in a partial class to configure DI, database, auth, etc.</summary>");
914+
sb.AppendLine(" static partial void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder);");
915+
sb.AppendLine(" /// <summary>Override in a partial class to add headers, tokens, etc.</summary>");
916+
sb.AppendLine(" static partial void ConfigureClient(HttpClient client);");
891917

892918
// GET list — verify returns items array
893919
if ((info.Operations & OpGetList) != 0)

0 commit comments

Comments
 (0)