fix
This commit is contained in:
189
WebApi.Test/Application/EmailMessageApplicationTest.cs
Normal file
189
WebApi.Test/Application/EmailMessageApplicationTest.cs
Normal file
@@ -0,0 +1,189 @@
|
||||
using Application.Dto.Email;
|
||||
using Service.EmailServices;
|
||||
|
||||
namespace WebApi.Test.Application;
|
||||
|
||||
/// <summary>
|
||||
/// EmailMessageApplication 单元测试
|
||||
/// </summary>
|
||||
public class EmailMessageApplicationTest : BaseApplicationTest
|
||||
{
|
||||
private readonly IEmailMessageRepository _emailRepository;
|
||||
private readonly ITransactionRecordRepository _transactionRepository;
|
||||
private readonly IEmailHandleService _emailHandleService;
|
||||
private readonly IEmailSyncService _emailSyncService;
|
||||
private readonly ILogger<EmailMessageApplication> _logger;
|
||||
private readonly EmailMessageApplication _application;
|
||||
|
||||
public EmailMessageApplicationTest()
|
||||
{
|
||||
_emailRepository = Substitute.For<IEmailMessageRepository>();
|
||||
_transactionRepository = Substitute.For<ITransactionRecordRepository>();
|
||||
_emailHandleService = Substitute.For<IEmailHandleService>();
|
||||
_emailSyncService = Substitute.For<IEmailSyncService>();
|
||||
_logger = CreateMockLogger<EmailMessageApplication>();
|
||||
_application = new EmailMessageApplication(
|
||||
_emailRepository,
|
||||
_transactionRepository,
|
||||
_emailHandleService,
|
||||
_emailSyncService,
|
||||
_logger);
|
||||
}
|
||||
|
||||
#region GetListAsync Tests
|
||||
|
||||
[Fact]
|
||||
public async Task GetListAsync_正常查询_应返回邮件列表()
|
||||
{
|
||||
// Arrange
|
||||
var emailList = new List<EmailMessage>
|
||||
{
|
||||
new() { Id = 1, Subject = "测试邮件1", From = "test@example.com", ReceivedDate = DateTime.Now },
|
||||
new() { Id = 2, Subject = "测试邮件2", From = "test2@example.com", ReceivedDate = DateTime.Now }
|
||||
};
|
||||
|
||||
_emailRepository.GetPagedListAsync(Arg.Any<DateTime?>(), Arg.Any<long?>())
|
||||
.Returns((emailList, DateTime.Now, 2L));
|
||||
_emailRepository.GetTotalCountAsync().Returns(2L);
|
||||
_transactionRepository.GetCountByEmailIdAsync(Arg.Any<long>()).Returns(5);
|
||||
|
||||
var request = new EmailQueryRequest();
|
||||
|
||||
// Act
|
||||
var result = await _application.GetListAsync(request);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Data.Should().HaveCount(2);
|
||||
result.Total.Should().Be(2);
|
||||
result.Data[0].TransactionCount.Should().Be(5);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetByIdAsync Tests
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_存在的邮件_应返回邮件详情()
|
||||
{
|
||||
// Arrange
|
||||
var email = new EmailMessage
|
||||
{
|
||||
Id = 1,
|
||||
Subject = "测试邮件",
|
||||
From = "test@example.com",
|
||||
Body = "邮件内容",
|
||||
To = "接收人 <receiver@example.com>",
|
||||
ReceivedDate = DateTime.Now
|
||||
};
|
||||
|
||||
_emailRepository.GetByIdAsync(1).Returns(email);
|
||||
_transactionRepository.GetCountByEmailIdAsync(1).Returns(3);
|
||||
|
||||
// Act
|
||||
var result = await _application.GetByIdAsync(1);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Id.Should().Be(1);
|
||||
result.Subject.Should().Be("测试邮件");
|
||||
result.TransactionCount.Should().Be(3);
|
||||
result.ToName.Should().Be("接收人");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_邮件不存在_应抛出NotFoundException()
|
||||
{
|
||||
// Arrange
|
||||
_emailRepository.GetByIdAsync(999).Returns((EmailMessage?)null);
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<NotFoundException>(() => _application.GetByIdAsync(999));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DeleteByIdAsync Tests
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteByIdAsync_成功删除_应不抛出异常()
|
||||
{
|
||||
// Arrange
|
||||
_emailRepository.DeleteAsync(1).Returns(true);
|
||||
|
||||
// Act
|
||||
await _application.DeleteByIdAsync(1);
|
||||
|
||||
// Assert
|
||||
await _emailRepository.Received(1).DeleteAsync(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteByIdAsync_删除失败_应抛出BusinessException()
|
||||
{
|
||||
// Arrange
|
||||
_emailRepository.DeleteAsync(999).Returns(false);
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<BusinessException>(() => _application.DeleteByIdAsync(999));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RefreshTransactionRecordsAsync Tests
|
||||
|
||||
[Fact]
|
||||
public async Task RefreshTransactionRecordsAsync_邮件存在且刷新成功_应不抛出异常()
|
||||
{
|
||||
// Arrange
|
||||
var email = new EmailMessage { Id = 1, Subject = "测试邮件" };
|
||||
_emailRepository.GetByIdAsync(1).Returns(email);
|
||||
_emailHandleService.RefreshTransactionRecordsAsync(1).Returns(true);
|
||||
|
||||
// Act
|
||||
await _application.RefreshTransactionRecordsAsync(1);
|
||||
|
||||
// Assert
|
||||
await _emailHandleService.Received(1).RefreshTransactionRecordsAsync(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RefreshTransactionRecordsAsync_邮件不存在_应抛出NotFoundException()
|
||||
{
|
||||
// Arrange
|
||||
_emailRepository.GetByIdAsync(999).Returns((EmailMessage?)null);
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<NotFoundException>(() =>
|
||||
_application.RefreshTransactionRecordsAsync(999));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RefreshTransactionRecordsAsync_刷新失败_应抛出BusinessException()
|
||||
{
|
||||
// Arrange
|
||||
var email = new EmailMessage { Id = 1, Subject = "测试邮件" };
|
||||
_emailRepository.GetByIdAsync(1).Returns(email);
|
||||
_emailHandleService.RefreshTransactionRecordsAsync(1).Returns(false);
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<BusinessException>(() =>
|
||||
_application.RefreshTransactionRecordsAsync(1));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SyncEmailsAsync Tests
|
||||
|
||||
[Fact]
|
||||
public async Task SyncEmailsAsync_应调用邮件同步服务()
|
||||
{
|
||||
// Act
|
||||
await _application.SyncEmailsAsync();
|
||||
|
||||
// Assert
|
||||
await _emailSyncService.Received(1).SyncEmailsAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user