Files
DMS/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs

51 lines
1.6 KiB
C#
Raw Normal View History

2025-07-21 23:04:28 +08:00
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 JetBrains.Annotations;
namespace DMS.Infrastructure.UnitTests.Services;
[TestSubject(typeof(DeviceService))]
public class DeviceServiceTest
{
private readonly DeviceRepository _deviceRepository;
private readonly DeviceService _deviceService;
private readonly IMapper _mapper;
public DeviceServiceTest()
{
// 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);
_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);
}
}