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:
@@ -272,6 +272,7 @@ describe('VerificationService', () => {
|
||||
};
|
||||
|
||||
mockRedis.get.mockResolvedValue(JSON.stringify(codeInfo));
|
||||
mockRedis.ttl.mockResolvedValue(240); // Mock TTL 返回 240 秒
|
||||
|
||||
await expect(service.verifyCode(email, type, '654321')).rejects.toThrow(
|
||||
new BadRequestException('验证码错误,剩余尝试次数: 1')
|
||||
@@ -285,7 +286,7 @@ describe('VerificationService', () => {
|
||||
expect(mockRedis.set).toHaveBeenCalledWith(
|
||||
`verification_code:${type}:${email}`,
|
||||
JSON.stringify(updatedCodeInfo),
|
||||
300
|
||||
240
|
||||
);
|
||||
});
|
||||
|
||||
@@ -298,6 +299,7 @@ describe('VerificationService', () => {
|
||||
};
|
||||
|
||||
mockRedis.get.mockResolvedValue(JSON.stringify(codeInfo));
|
||||
mockRedis.ttl.mockResolvedValue(240); // Mock TTL 返回 240 秒
|
||||
|
||||
await expect(service.verifyCode(email, type, '654321')).rejects.toThrow(
|
||||
new BadRequestException('验证码错误,剩余尝试次数: 0')
|
||||
@@ -391,18 +393,20 @@ describe('VerificationService', () => {
|
||||
ttl: 240,
|
||||
attempts: 1,
|
||||
maxAttempts: 3,
|
||||
code: '123456',
|
||||
createdAt: expect.any(Number),
|
||||
});
|
||||
});
|
||||
|
||||
it('应该在验证码不存在时返回基本信息', async () => {
|
||||
mockRedis.exists.mockResolvedValue(false);
|
||||
mockRedis.ttl.mockResolvedValue(-1);
|
||||
mockRedis.ttl.mockResolvedValue(-2); // -2 表示键不存在
|
||||
|
||||
const result = await service.getCodeStats(email, type);
|
||||
|
||||
expect(result).toEqual({
|
||||
exists: false,
|
||||
ttl: -1,
|
||||
ttl: -2, // 修改为 -2
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user