Files
cats.controller.spec.ts
What you'll learn
- Test a controller as a class: spy on the injected service and assert what the handler passed on and returned
- Test a guard in isolation with a fake ExecutionContext and real metadata read through the Reflector
- Know what a unit test of a controller cannot see, and leave routing, pipes and status codes to end-to-end tests
Press Run tests to start the app. Its log appears here.Tests
- cats/cats.controller.spec.tsrun to see its tests
- auth/roles.guard.spec.tsrun to see its tests
Bugs your tests must catch
Each of these replaces one file with a buggy version and runs your tests again. A bug is caught when at least one test fails against it. Tests that pass whatever the code does are not tests.
- create passes age 0 to the servicecats/cats.controller.ts
- findOne always asks for cat 1cats/cats.controller.ts
- a missing user is refused with 403 instead of 401auth/roles.guard.ts
- the guard demands every required role, not one of themauth/roles.guard.ts
- a handler without @Roles is treated as admin-onlyauth/roles.guard.ts