# StopIteration: 由 side_effect 列表耗尽引发

- **ID:** `python/unittest-mock-side-effect`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

mock 的 side_effect 被设置为有限列表，而被测代码调用 mock 的次数超过列表长度，引发 StopIteration。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Use a callable for side_effect that handles any number of calls:

m.side_effect = lambda x: x * 2
   ```
2. **** (88% 成功率)
   ```
   Extend the list to match the expected call count and assert:

m.side_effect = [1, 2, 3]
run()
assert m.call_count == 3
   ```

## 无效尝试

- **** — This hides the real issue: the test exercises more calls than expected, or the list is too short. (85% 失败率)
- **** — Adding one None only delays the problem; a subsequent call raises StopIteration again. (80% 失败率)
