Files
whale-town-end/src/core/utils/verification/verification.module.spec.ts
moyin 75ce4a2778 test(verification): 添加VerificationModule测试文件
范围:src/core/utils/verification/verification.module.spec.ts
- 新增VerificationModule的完整测试覆盖
- 包含模块定义、服务提供者、依赖关系和导出功能的测试
- 确保模块配置的正确性和完整性
- 提升verification模块的测试覆盖率
2026-01-12 19:22:19 +08:00

126 lines
3.6 KiB
TypeScript
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.
/**
* 验证模块测试套件
*
* 功能描述:
* - 测试VerificationModule的模块配置和依赖注入
* - 验证模块导入、提供者和导出的正确性
* - 确保验证服务的正确配置
* - 测试模块间的依赖关系
*
* 测试覆盖范围:
* - 模块实例化:模块能够正确创建和初始化
* - 依赖注入:所有服务的正确注入
* - 服务导出VerificationService的正确导出
* - 配置验证:验证码配置的正确性
*
* 最近修改:
* - 2026-01-12: 功能新增 - 创建VerificationModule测试文件确保模块配置测试覆盖 (修改者: moyin)
*
* @author moyin
* @version 1.0.0
* @since 2026-01-12
* @lastModified 2026-01-12
*/
import { Test, TestingModule } from '@nestjs/testing';
import { ConfigService } from '@nestjs/config';
import { VerificationModule } from './verification.module';
import { VerificationService } from './verification.service';
describe('VerificationModule', () => {
let module: TestingModule;
let verificationService: VerificationService;
let configService: ConfigService;
beforeEach(async () => {
const mockConfigService = {
get: jest.fn((key: string, defaultValue?: any) => {
switch (key) {
case 'VERIFICATION_CODE_LENGTH':
return 6;
case 'VERIFICATION_CODE_EXPIRES':
return 300;
case 'VERIFICATION_COOLDOWN':
return 60;
default:
return defaultValue;
}
}),
};
module = await Test.createTestingModule({
providers: [
VerificationService,
{
provide: ConfigService,
useValue: mockConfigService,
},
{
provide: 'REDIS_SERVICE',
useValue: {
get: jest.fn(),
set: jest.fn(),
del: jest.fn(),
exists: jest.fn(),
},
},
],
}).compile();
verificationService = module.get<VerificationService>(VerificationService);
configService = module.get<ConfigService>(ConfigService);
});
afterEach(async () => {
if (module) {
await module.close();
}
});
it('should be defined', () => {
expect(module).toBeDefined();
});
describe('Service Providers', () => {
it('should provide VerificationService', () => {
expect(verificationService).toBeDefined();
expect(verificationService).toBeInstanceOf(VerificationService);
});
it('should provide ConfigService', () => {
expect(configService).toBeDefined();
expect(configService.get).toBeDefined();
});
});
describe('Module Dependencies', () => {
it('should import required modules', () => {
expect(module).toBeDefined();
expect(verificationService).toBeDefined();
});
it('should not have circular dependencies', () => {
expect(module).toBeDefined();
});
});
describe('Module Exports', () => {
it('should export VerificationService', () => {
expect(verificationService).toBeDefined();
expect(verificationService).toBeInstanceOf(VerificationService);
});
it('should make VerificationService available for injection', () => {
const service = module.get<VerificationService>(VerificationService);
expect(service).toBe(verificationService);
});
});
describe('Configuration Validation', () => {
it('should validate verification configuration completeness', () => {
expect(configService.get('VERIFICATION_CODE_LENGTH')).toBeDefined();
expect(configService.get('VERIFICATION_CODE_EXPIRES')).toBeDefined();
expect(configService.get('VERIFICATION_COOLDOWN')).toBeDefined();
});
});
});