完成新建变量表的功能测试

This commit is contained in:
2025-07-24 19:51:16 +08:00
parent 899e657053
commit 4e26685560
9 changed files with 163 additions and 6 deletions

View File

@@ -0,0 +1,67 @@
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Application.Services;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
namespace DMS.Infrastructure.UnitTests.Services;
[TestSubject(typeof(DeviceAppService))]
public class DeviceAppServiceTest : BaseServiceTest // 继承基类
{
private readonly IDeviceAppService _deviceService;
public DeviceAppServiceTest() : base()
{
// 从 IoC 容器中解析出需要测试的服务
// 使用 GetRequiredService 可以确保如果服务未注册,测试会立即失败,这通常是我们想要的。
_deviceService = ServiceProvider.GetRequiredService<IDeviceAppService>();
}
[Fact]
public async Task CreateDeviceWithDetailsAsyncTest()
{
// Arrange
var dto = new CreateDeviceWithDetailsDto
{
Device = FakerHelper.FakeCreateDeviceDto(),
VariableTable = FakerHelper.FakeVariableTableDto(),
DeviceMenu = FakerHelper.FakeCreateMenuDto(),
VariableTableMenu = FakerHelper.FakeCreateMenuDto()
// ... 填充其他需要的数据
};
// Act
var addedDeviceId = await _deviceService.CreateDeviceWithDetailsAsync(dto);
// Assert
Assert.NotEqual(0, addedDeviceId);
}
[Fact]
public async Task DeleteDeviceAsyncTest()
{
// Act
var isSuccess = await _deviceService.DeleteDeviceByIdAsync(4);
// Assert
Assert.Equal(isSuccess,true);
}
[Fact]
public async Task UpdateDeviceAsyncTest()
{
UpdateDeviceDto dto = new UpdateDeviceDto()
{
Id = 5,
Name = "lalala",
IsActive = true,
Rack = 0,
Slot = 0
};
var res = await _deviceService.UpdateDeviceAsync(dto);
Assert.NotEqual(res,0);
}
}