test(email, verification, login): 更新测试中的断言内容, 修复测试error.

- Replace boolean assertions with structured result object checks in email service tests
- Update email service tests to verify success flag and isTestMode property
- Add error message assertions for failed email sending scenarios
- Change logger spy from 'log' to 'warn' for test mode email output
- Update test message to clarify emails are not actually sent in test mode
- Add code and createdAt properties to verification code stats mock data
- Fix TTL mock value from -1 to -2 to correctly represent non-existent keys
- Replace Inject decorator with direct UsersService type injection in LoginCoreService
- Ensure verification service tests properly mock TTL values during code verification
- Improve test coverage by validating complete response structures instead of simple booleans
This commit is contained in:
angjustinl
2025-12-18 13:29:55 +08:00
parent 26ea5ac815
commit 6dece752ef
3 changed files with 31 additions and 16 deletions

View File

@@ -117,7 +117,8 @@ describe('EmailService', () => {
const result = await service.sendEmail(emailOptions);
expect(result).toBe(true);
expect(result.success).toBe(true);
expect(result.isTestMode).toBe(false);
expect(mockTransporter.sendMail).toHaveBeenCalledWith({
from: '"Test Sender" <noreply@test.com>',
to: 'test@example.com',
@@ -138,7 +139,8 @@ describe('EmailService', () => {
const result = await service.sendEmail(emailOptions);
expect(result).toBe(false);
expect(result.success).toBe(false);
expect(result.error).toBe('发送失败');
});
it('应该在测试模式下输出邮件内容', async () => {
@@ -157,13 +159,14 @@ describe('EmailService', () => {
};
// Mock the service to use test transporter
const loggerSpy = jest.spyOn(service['logger'], 'log').mockImplementation();
const loggerSpy = jest.spyOn(service['logger'], 'warn').mockImplementation();
service['transporter'] = testTransporter;
const result = await service.sendEmail(emailOptions);
expect(result).toBe(true);
expect(loggerSpy).toHaveBeenCalledWith('=== 邮件发送(测试模式) ===');
expect(result.success).toBe(true);
expect(result.isTestMode).toBe(true);
expect(loggerSpy).toHaveBeenCalledWith('=== 邮件发送(测试模式 - 邮件未真实发送) ===');
loggerSpy.mockRestore();
});
@@ -183,7 +186,8 @@ describe('EmailService', () => {
const result = await service.sendVerificationCode(options);
expect(result).toBe(true);
expect(result.success).toBe(true);
expect(result.isTestMode).toBe(false);
expect(mockTransporter.sendMail).toHaveBeenCalledWith(
expect.objectContaining({
to: 'test@example.com',
@@ -206,7 +210,8 @@ describe('EmailService', () => {
const result = await service.sendVerificationCode(options);
expect(result).toBe(true);
expect(result.success).toBe(true);
expect(result.isTestMode).toBe(false);
expect(mockTransporter.sendMail).toHaveBeenCalledWith(
expect.objectContaining({
to: 'test@example.com',
@@ -227,7 +232,8 @@ describe('EmailService', () => {
const result = await service.sendVerificationCode(options);
expect(result).toBe(false);
expect(result.success).toBe(false);
expect(result.error).toBe('发送失败');
});
});
@@ -238,7 +244,8 @@ describe('EmailService', () => {
const result = await service.sendWelcomeEmail('test@example.com', '测试用户');
expect(result).toBe(true);
expect(result.success).toBe(true);
expect(result.isTestMode).toBe(false);
expect(mockTransporter.sendMail).toHaveBeenCalledWith(
expect.objectContaining({
to: 'test@example.com',
@@ -253,7 +260,8 @@ describe('EmailService', () => {
const result = await service.sendWelcomeEmail('test@example.com', '测试用户');
expect(result).toBe(false);
expect(result.success).toBe(false);
expect(result.error).toBe('发送失败');
});
});
@@ -358,7 +366,8 @@ describe('EmailService', () => {
const result = await service.sendEmail(emailOptions);
expect(result).toBe(false);
expect(result.success).toBe(false);
expect(result.error).toBe('ECONNREFUSED');
});
it('应该正确处理认证错误', async () => {
@@ -372,7 +381,8 @@ describe('EmailService', () => {
const result = await service.sendEmail(emailOptions);
expect(result).toBe(false);
expect(result.success).toBe(false);
expect(result.error).toBe('Invalid login');
});
it('应该正确处理连接验证错误', async () => {