可测试性 Interface 设计
好 interface 使测试 natural:
- 接受 dependency,不要内部创建
```typescript
// Testable
function processOrder(order, paymentGateway) {}
// Hard to test
function processOrder(order) {
const gateway = new StripeGateway();
}
```
- 返回结果,不要产生 side effect
```typescript
// Testable
function calculateDiscount(cart): Discount {}
// Hard to test
function applyDiscount(cart): void {
cart.total -= discount;
}
```
- 小 surface area
- 方法越少 = 所需 test 越少
- 参数越少 = test setup 越简单