Fixing Invalid Referral Status and Raising Test Coverage in the Brokers API (NestJS)
说明与用途
在 NestJS 控制器中实现了枚举验证,修复了 SQL 错误,并扩展了单元测试,将覆盖率提升至 80% 以上,消除了因无效推荐状态导致的 500 错误。
问题
BrokersController threw 500 errors when referral status was not a valid enum value, and a SQL typo caused query failures. The test coverage for brokers and communications was below 3%, allowing bugs to slip through.
结果
Implemented robust enum validation, corrected SQL typo, and expanded unit tests, raising test coverage from ~1% to >80% and eliminating 500 errors caused by invalid referral statuses.
工作流
- Align controller with ReferralStatus enum using ParseEnumPipe
- Replace hard‑coded status guard with automatic enum validation
- Fix SQL query to use correct alias 'total' instead of 'count'
- Create brokers.test.ts covering CRUD, agent management, portal flows, and status updates
- Create communications.test.ts covering email logic with sendMail mock
- Run tests to confirm 400 on invalid status and 200 on valid status
- Verify coverage reports exceed 80%
- Commit changes and push to repository
证据
The pipe throws a BadRequestException automatically if the payload isn’t a valid enum value
A snippet from brokers.test.ts that verifies enum validation
- 记录日期: 2026-09-20T02:30:18.935Z