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) { $res = $res.Replace("{E}", $e) } 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 { Username = "admin" }; user.IsActive.Should().BeTrue(); } } '@) } else { # Jenerik Entity Testi Write-TestFile "$ProjectName.Application.Tests\Features\{E}s\Add{E}Tests.cs" (T @' using FluentAssertions; using Moq; using __PROJ__.Application.Features.{E}s.Commands.Add{E}; using __PROJ__.Domain.Entities; using __PROJ__.Domain.Interfaces; using Xunit; namespace __PROJ__.Application.Tests.Features.{E}s; 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("Test"), 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(); } } '@) |