modules/testing/Write-TestLayer.ps1
|
param( [string]$ProjectName, [string]$OrmMode, [string]$OutputPath, [string[]]$Entities ) . (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Utils.ps1") $tests = Join-Path $OutputPath "tests" function Write-TestFile($rel, $con) { Write-CoreFile (Join-Path $tests $rel) $con } function T($t, $e = "") { $res = $t.Replace("__PROJ__", $ProjectName) if ($e) { $pe = Get-Plural $e $cmdArgs = if ($e -eq "Product") { '"Test", 99.90m' } else { '"Test"' } $res = $res.Replace("{PE}", $pe).Replace("{E}", $e).Replace("{CMD_ARGS}", $cmdArgs) } return $res } foreach ($e in $Entities) { if ($e -eq "User") { Write-TestFile "$ProjectName.Domain.Tests\Entities\UserTests.cs" (T @' using FluentAssertions; using __PROJ__.Domain.Entities; using Xunit; namespace __PROJ__.Domain.Tests.Entities; public class UserTests { [Fact] public void NewUser_ShouldHave_DefaultValues() { var user = new User("admin", "hashed_pass", new __PROJ__.Domain.ValueObjects.Email("admin@example.com")); user.IsActive.Should().BeTrue(); } } '@) } else { # Jenerik Entity Testi $pe = Get-Plural $e Write-TestFile "$ProjectName.Application.Tests\Features\$pe\Add$e`Tests.cs" (T @' using FluentAssertions; using Moq; using __PROJ__.Application.Features.{PE}.Commands.Add{E}; using __PROJ__.Domain.Entities; using __PROJ__.Domain.Interfaces; using Xunit; namespace __PROJ__.Application.Tests.Features.{PE}; public class Add{E}Tests { [Fact] public async Task Handle_Valid{E}_ShouldReturn_Id() { var mockRepo = new Mock<I{E}Repository>(); mockRepo.Setup(x => x.AddAsync(It.IsAny<{E}>())).ReturnsAsync(1); var handler = new Add{E}CommandHandler(mockRepo.Object); var result = await handler.Handle(new Add{E}Command({CMD_ARGS}), CancellationToken.None); result.Should().Be(1); } } '@ $e) } } # Integration Test (Minimal) Write-TestFile "$ProjectName.Integration.Tests\Api\HealthTests.cs" (T @' using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; using Xunit; namespace __PROJ__.Integration.Tests.Api; public class HealthTests : IClassFixture<WebApplicationFactory<Program>> { private readonly WebApplicationFactory<Program> _factory; public HealthTests(WebApplicationFactory<Program> factory) => _factory = factory; [Fact] public async Task Root_ShouldReturn_Success() { var client = _factory.CreateClient(); var response = await client.GetAsync("/swagger/index.html"); response.IsSuccessStatusCode.Should().BeTrue(); } } '@) |