Files
DMS/DMS.Infrastructure.UnitTests/Services/BaseServiceTest.cs
David P.G ec1f94a898 1 refactor(config): 将AppSettings移至Application层并更新配置结构
2
    3 - 将AppSettings从DMS.Infrastructure移至DMS.Application
    4 - 将DatabaseSettings重命名为AppSettings.Database并更新所有引用
    5 - 将配置文件从appSettings.json更改为dms_config.json
    6 - 更新所有项目引用以使用新的AppSettings命名空间
    7 - 移除DI容器中的SqlSugarDbContext直接实例化
    8 - 添加Material Design Icons字体并更新设置视图UI
    9 - 通过移除不必要的变量映射更新来优化S7服务
   10 - 将数据库连接字符串属性名从Database更新为DbName
2025-10-04 18:42:12 +08:00

65 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// DMS.Infrastructure.UnitTests/Services/BaseServiceTest.cs
using AutoMapper;
using AutoMapper.Internal;
using DMS.Application.Configurations;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Database;
using DMS.Application.Services;
using DMS.Application.Services.Database;
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Repositories;
using Microsoft.Extensions.DependencyInjection;
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>();
// --- 注册服务和仓储 ---
// 使用 Transient 或 Scoped 取决于服务的生命周期需求对于测试Transient 通常更安全。
// 注册仓储管理器
services.AddTransient<IRepositoryManager, RepositoryManager>();
services.AddTransient<IInitializeRepository, InitializeRepository>();
// 注册应用服务
services.AddTransient<IDeviceAppService, DeviceAppService>();
services.AddTransient<IVariableTableAppService, VariableTableAppService>();
services.AddTransient<IVariableAppService, VariableAppService>();
// services.AddTransient<IVariableAppService, VariableAppService>(); // 如果需要测试 VariableService取消此行注释
// ... 在这里注册所有其他的应用服务 ...
// --- 构建服务提供程序 ---
ServiceProvider = services.BuildServiceProvider();
// 验证 AutoMapper 配置 (可选,但强烈推荐)
var mapper = ServiceProvider.GetService<IMapper>();
mapper?.ConfigurationProvider.AssertConfigurationIsValid();
}
}