# 断言错误：预期模拟被调用 2 次。实际被调用 1 次。

- **ID:** `python/unittest-mock-call-count-error`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

被测试的代码调用模拟函数的次数少于预期，通常是由于条件逻辑或提前返回。

## 版本兼容性

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

## 解决方案

1. **Debug the code to understand why the function is not called enough times.** (90% 成功率)
   ```
   Add print statements or use a debugger to trace the execution path.
   ```
2. **Use mock.call_args_list to inspect actual calls and compare with expected.** (85% 成功率)
   ```
   assert mock.call_args_list == [call(arg1), call(arg2)]
   ```

## 无效尝试

- **Increasing the expected call count to match actual calls** — This masks the bug in the code that is not calling the function enough times. (80% 失败率)
- **Adding mock.reset_mock() before assertion** — Resetting the mock clears call history, making the assertion pass incorrectly. (70% 失败率)
