# StopIteration: 
# 在测试中 side_effect 列表耗尽后引发

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

## 根因

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

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   mock.get.side_effect = lambda key: {'a': 1, 'b': 2}.get(key, 0)
   ```
2. **** (88% 成功率)
   ```
   from itertools import chain, repeat
mock.get.side_effect = chain([1, 2], repeat(0))
   ```

## 无效尝试

- **** — Fixes the current call count but breaks again when the code loops over a different-sized input. (75% 失败率)
- **** — Loses the ability to return different values per call, which is often the whole point of the test. (70% 失败率)
