完成新建设备的单元 测试包括,添加变量表,和添加菜单

This commit is contained in:
2025-07-24 15:07:03 +08:00
parent b0d5db3626
commit ac38128e4d
33 changed files with 497 additions and 416 deletions

View File

@@ -0,0 +1,64 @@
// DMS.Infrastructure.UnitTests/Services/BaseServiceTest.cs
using AutoMapper;
using AutoMapper.Internal;
using DMS.Application.Interfaces;
using DMS.Application.Services;
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Repositories;
using Microsoft.Extensions.DependencyInjection;
using DMS.Infrastructure.Configurations;
namespace DMS.Infrastructure.UnitTests.Services;
public class BaseServiceTest
{
// ServiceProvider 将是所有测试的服务容器
protected readonly IServiceProvider ServiceProvider;
public BaseServiceTest()
{
var services = new ServiceCollection();
// --- 核心配置 ---
services.AddAutoMapper(cfg =>
{
// 最终解决方案根据异常信息的建议设置此标记以忽略重复的Profile加载。
// 注意:此属性位于 Internal() 方法下。
cfg.Internal().AllowAdditiveTypeMapCreation = true;
cfg.AddProfile(new DMS.Application.Profiles.MappingProfile());
cfg.AddProfile(new DMS.Infrastructure.Profiles.MappingProfile());
});
// 2. 配置数据库上下文 (在测试中通常使用单例)
services.AddSingleton<SqlSugarDbContext>(_ =>
{
var appSettings = new AppSettings { Database = { Database = "dms_test" } };
return new SqlSugarDbContext(appSettings);
});
// --- 注册服务和仓储 ---
// 使用 Transient 或 Scoped 取决于服务的生命周期需求对于测试Transient 通常更安全。
// 注册仓储管理器
services.AddTransient<IRepositoryManager, RepositoryManager>();
services.AddTransient<IInitializeRepository, InitializeRepository>();
// 注册应用服务
services.AddTransient<IDeviceAppService, DeviceService>();
// services.AddTransient<IVariableAppService, VariableAppService>(); // 如果需要测试 VariableService取消此行注释
// ... 在这里注册所有其他的应用服务 ...
// --- 构建服务提供程序 ---
ServiceProvider = services.BuildServiceProvider();
// 验证 AutoMapper 配置 (可选,但强烈推荐)
var mapper = ServiceProvider.GetService<IMapper>();
mapper?.ConfigurationProvider.AssertConfigurationIsValid();
}
}

View File

@@ -1,31 +0,0 @@
using DMS.Core.Interfaces;
using DMS.Infrastructure.Configurations;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Services;
namespace DMS.Infrastructure.UnitTests.Services;
public class DatabaseServiceTest
{
private IDatabaseService _databaseService;
public DatabaseServiceTest()
{
AppSettings appSettings = new AppSettings();
appSettings.Database.Database = "dms_test";
SqlSugarDbContext dbContext=new SqlSugarDbContext(appSettings);
_databaseService = new DatabaseService(dbContext);
}
[Fact]
public void InitializeTables_Test()
{
_databaseService.InitializeTables();
Assert.True(_databaseService.IsAnyTable("dbdevice"));
}
[Fact]
public void InitializeTableIndex_Test()
{
_databaseService.InitializeTableIndex();
}
}

View File

@@ -1,73 +1,41 @@
using AutoMapper;
using DMS.Core.Models;
using DMS.Infrastructure.Configurations;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Profiles;
using DMS.Infrastructure.Repositories;
using DMS.Infrastructure.Services;
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(DeviceService))]
public class DeviceServiceTest
public class DeviceServiceTest : BaseServiceTest // 继承基类
{
private readonly DeviceRepository _deviceRepository;
private readonly DeviceService _deviceService;
private readonly IMapper _mapper;
private readonly IDeviceAppService _deviceService;
public DeviceServiceTest()
public DeviceServiceTest() : base()
{
// 1. 创建 MapperConfiguration
var mappingConfig = new MapperConfiguration(mc =>
// 从 IoC 容器中解析出需要测试的服务
// 使用 GetRequiredService 可以确保如果服务未注册,测试会立即失败,这通常是我们想要的。
_deviceService = ServiceProvider.GetRequiredService<IDeviceAppService>();
}
[Fact]
public async Task CreateDeviceWithDetailsAsyncTest()
{
// Arrange
var dto = new CreateDeviceWithDetailsDto
{
// 添加你的所有 Profile
mc.AddProfile(new MappingProfile());
// 如果有其他 Profile也可以在这里添加
// mc.AddProfile(new AnotherProfile());
});
Device = FakerHelper.FakeCreateDeviceDto(),
VariableTable = FakerHelper.FakeVariableTableDto(),
DeviceMenu = FakerHelper.FakeCreateMenuDto(),
VariableTableMenu = FakerHelper.FakeCreateMenuDto()
// ... 填充其他需要的数据
};
// 2. 验证映射配置是否有效 (可选,但在开发环境中推荐)
mappingConfig.AssertConfigurationIsValid();
// 3. 创建 IMapper 实例
_mapper = mappingConfig.CreateMapper();
// Act
var addedDeviceId = await _deviceService.CreateDeviceWithDetailsAsync(dto);
AppSettings appSettings = new AppSettings();
appSettings.Database.Database = "dms_test";
SqlSugarDbContext dbContext = new SqlSugarDbContext(appSettings);
_deviceRepository= new DeviceRepository(_mapper,dbContext);
_deviceService = new DeviceService(_deviceRepository);
}
[Fact]
public async Task AddAsync_Test()
{
var dbDevice = FakerHelper.FakeDbDevice();
var addDevice= await _deviceService.AddAsync(_mapper.Map<Device>(dbDevice));
Assert.NotEqual(0, addDevice.Id);
}
[Fact]
public async Task TakeAsync_Test()
{
var device= await _deviceService.TakeAsync(2);
Assert.Equal(2,device.Count);
}
[Fact]
public async Task UpdateAsync_Test()
{
var devices= await _deviceService.TakeAsync(1);
devices[0].IpAddress = "127.0.0.1";
var res= await _deviceService.UpdateAsync(devices[0]);
Assert.Equal(1,res);
}
[Fact]
public async Task DeleteAsync_Test()
{
var devices= await _deviceService.TakeAsync(1);
var res= await _deviceService.DeleteAsync(devices[0]);
Assert.Equal(1,res);
// Assert
Assert.NotEqual(0, addedDeviceId);
}
}

View File

@@ -0,0 +1,24 @@
using DMS.Application.Interfaces;
using DMS.Application.Services;
using DMS.Core.Interfaces.Repositories;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
namespace DMS.Infrastructure.UnitTests.Services;
[TestSubject(typeof(InitializeService))]
public class InitializeServiceTest:BaseServiceTest
{
private readonly IInitializeRepository _initializeRepository;
public InitializeServiceTest()
{
_initializeRepository = ServiceProvider.GetRequiredService<IInitializeRepository>();
}
[Fact]
public void InitializeTablesTest()
{
_initializeRepository.InitializeTables();
}
}

View File

@@ -1,71 +0,0 @@
using AutoMapper;
using DMS.Core.Models;
using DMS.Infrastructure.Configurations;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Profiles;
using DMS.Infrastructure.Repositories;
using DMS.Infrastructure.Services;
namespace DMS.Infrastructure.UnitTests.Services;
public class VariableTableServieTest
{
private readonly VariableTableRepository _variableTableRepository;
private readonly VariableTableService _variableTableService;
private readonly IMapper _mapper;
public VariableTableServieTest()
{
// 1. 创建 MapperConfiguration
var mappingConfig = new MapperConfiguration(mc =>
{
// 添加你的所有 Profile
mc.AddProfile(new MappingProfile());
// 如果有其他 Profile也可以在这里添加
// mc.AddProfile(new AnotherProfile());
});
// 2. 验证映射配置是否有效 (可选,但在开发环境中推荐)
mappingConfig.AssertConfigurationIsValid();
// 3. 创建 IMapper 实例
_mapper = mappingConfig.CreateMapper();
AppSettings appSettings = new AppSettings();
appSettings.Database.Database = "dms_test";
SqlSugarDbContext dbContext = new SqlSugarDbContext(appSettings);
_variableTableRepository= new VariableTableRepository(_mapper,dbContext);
_variableTableService = new VariableTableService(_variableTableRepository);
}
[Fact]
public async Task AddAsync_Test()
{
// var dbDevice = FakerHelper
// var addDevice= await _variableTableService.AddAsync(_mapper.Map<Device>(dbDevice));
// Assert.NotEqual(0, addDevice.Id);
}
[Fact]
public async Task TakeAsync_Test()
{
var device= await _variableTableService.TakeAsync(2);
Assert.Equal(2,device.Count);
}
[Fact]
public async Task UpdateAsync_Test()
{
var devices= await _variableTableService.TakeAsync(1);
// devices[0].IpAddress = "127.0.0.1";
var res= await _variableTableService.UpdateAsync(devices[0]);
Assert.Equal(1,res);
}
[Fact]
public async Task DeleteAsync_Test()
{
var devices= await _variableTableService.TakeAsync(1);
var res= await _variableTableService.DeleteAsync(devices[0]);
Assert.Equal(1,res);
}
}