Unit tests init commit.

This commit is contained in:
emantzoros 2025-09-19 16:38:53 +03:00
parent 57df147702
commit c48c1e6ce4
6 changed files with 142 additions and 5 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@ Bullet6/bin/
Bullet6/obj/ Bullet6/obj/
BlazorApp.Shared/bin/ BlazorApp.Shared/bin/
.vs/BlazorApp/ .vs/BlazorApp/
BlazorApp.Tests/bin/
BlazorApp.Tests/obj/

View File

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bunit" Version="1.40.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BlazorApp\BlazorApp.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,104 @@
using BlazorApp.Controllers;
using BlazorApp.Interfaces.Services;
using BlazorApp.Shared.Models;
using BlazorApp.Shared.Models.Pagination;
using Microsoft.AspNetCore.Mvc;
using Moq;
namespace BlazorApp.Tests.Controllers;
public class CustomerControllerTests
{
private readonly Mock<ICustomerService> _mockService;
private readonly CustomerController _controller;
public CustomerControllerTests()
{
_mockService = new Mock<ICustomerService>();
_controller = new CustomerController(_mockService.Object);
}
[Fact]
public async Task Query_OK()
{
var customers = new PaginatedResult<Customer>
{
TotalCount = 1,
Results = new List<Customer>() { new Customer { Id = "blabla", CompanyName = "blabla" } }
};
_mockService.Setup(s => s.Query(It.IsAny<Shared.Queries.CustomerQuery>())).ReturnsAsync(customers);
var actionResult = await _controller.Query(new Shared.Queries.CustomerQuery());
var okResult = Assert.IsType<OkObjectResult>(actionResult.Result);
var returnResult = Assert.IsType<PaginatedResult<Customer>>(okResult.Value);
Assert.Single(returnResult.Results);
Assert.Equal(1, returnResult.TotalCount);
_mockService.Verify(s => s.Query(It.IsAny<Shared.Queries.CustomerQuery>()), Times.Once);
}
[Fact]
public async Task Get_OK()
{
var customer = new Customer
{
Id = "blabla",
CompanyName = "blabla"
};
_mockService.Setup(s => s.Get(It.IsAny<string>())).ReturnsAsync(customer);
var actionResult = await _controller.Get("blabla");
var okResult = Assert.IsType<OkObjectResult>(actionResult.Result);
var returnResult = Assert.IsType<Customer>(okResult.Value);
Assert.NotNull(returnResult);
_mockService.Verify(s => s.Get("blabla"), Times.Once);
}
[Fact]
public async Task Delete_ReturnsNoContent()
{
_mockService.Setup(s => s.Delete(It.IsAny<string>())).Returns(Task.CompletedTask);
var actionResult = await _controller.Delete("blabla");
Assert.IsType<NoContentResult>(actionResult);
_mockService.Verify(s => s.Delete("blabla"), Times.Once);
}
[Fact]
public async Task Save_ReturnsNoContent()
{
var customer = new Customer
{
Id = "blabla",
CompanyName = "blabla"
};
_mockService.Setup(s => s.Save(It.IsAny<Customer>())).Returns(Task.CompletedTask);
var actionResult = await _controller.Save(customer);
Assert.IsType<NoContentResult>(actionResult);
_mockService.Verify(s => s.Save(customer), Times.Once);
}
[Fact]
public async Task Update_ReturnsNoContent()
{
var customer = new Customer
{
Id = "blabla",
CompanyName = "blabla"
};
_mockService.Setup(s => s.Update(It.IsAny<Customer>())).Returns(Task.CompletedTask);
var actionResult = await _controller.Update(customer);
Assert.IsType<NoContentResult>(actionResult);
_mockService.Verify(s => s.Update(customer), Times.Once);
}
}

View File

@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp.Client", "BlazorA
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp.Shared", "BlazorApp.Shared\BlazorApp.Shared.csproj", "{C6627F01-4882-40B1-890C-D70AA74B9BD1}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp.Shared", "BlazorApp.Shared\BlazorApp.Shared.csproj", "{C6627F01-4882-40B1-890C-D70AA74B9BD1}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bullet6", "Bullet6\Bullet6.csproj", "{8FA67E47-DDAB-4712-B628-F904086B1F77}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bullet6", "Bullet6\Bullet6.csproj", "{8FA67E47-DDAB-4712-B628-F904086B1F77}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp.Tests", "BlazorApp.Tests\BlazorApp.Tests.csproj", "{80656871-78E1-49BF-8935-34F42BDCF6A6}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -33,6 +35,10 @@ Global
{8FA67E47-DDAB-4712-B628-F904086B1F77}.Debug|Any CPU.Build.0 = Debug|Any CPU {8FA67E47-DDAB-4712-B628-F904086B1F77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FA67E47-DDAB-4712-B628-F904086B1F77}.Release|Any CPU.ActiveCfg = Release|Any CPU {8FA67E47-DDAB-4712-B628-F904086B1F77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FA67E47-DDAB-4712-B628-F904086B1F77}.Release|Any CPU.Build.0 = Release|Any CPU {8FA67E47-DDAB-4712-B628-F904086B1F77}.Release|Any CPU.Build.0 = Release|Any CPU
{80656871-78E1-49BF-8935-34F42BDCF6A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80656871-78E1-49BF-8935-34F42BDCF6A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80656871-78E1-49BF-8935-34F42BDCF6A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80656871-78E1-49BF-8935-34F42BDCF6A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -44,7 +44,6 @@ else
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
// Simulate asynchronous loading to demonstrate streaming rendering
await Task.Delay(500); await Task.Delay(500);
forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now)); forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
} }

View File

@ -12,7 +12,6 @@ using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents()
.AddInteractiveServerComponents() .AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents(); .AddInteractiveWebAssemblyComponents();
@ -43,7 +42,6 @@ var app = builder.Build();
app.ExecuteDbMigration(); app.ExecuteDbMigration();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseWebAssemblyDebugging(); app.UseWebAssemblyDebugging();
@ -51,7 +49,6 @@ if (app.Environment.IsDevelopment())
else else
{ {
app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts(); app.UseHsts();
} }