| 1 | # Interface Design for Testability |
| 2 | |
| 3 | Good interfaces make testing natural: |
| 4 | |
| 5 | 1. **Accept dependencies, don't create them** |
| 6 | |
| 7 | ```typescript |
| 8 | // Testable |
| 9 | function processOrder(order, paymentGateway) {} |
| 10 | |
| 11 | // Hard to test |
| 12 | function processOrder(order) { |
| 13 | const gateway = new StripeGateway(); |
| 14 | } |
| 15 | ``` |
| 16 | |
| 17 | 2. **Return results, don't produce side effects** |
| 18 | |
| 19 | ```typescript |
| 20 | // Testable |
| 21 | function calculateDiscount(cart): Discount {} |
| 22 | |
| 23 | // Hard to test |
| 24 | function applyDiscount(cart): void { |
| 25 | cart.total -= discount; |
| 26 | } |
| 27 | ``` |
| 28 | |
| 29 | 3. **Small surface area** |
| 30 | - Fewer methods = fewer tests needed |
| 31 | - Fewer params = simpler test setup |
| 32 |