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

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

## 根因

测试断言了 mock.assert_called_with 或 mock.assert_called_once，但实际调用次数与预期不符，通常是由于被测试代码中的条件逻辑。

## 版本兼容性

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

## 解决方案

1. **** (85% 成功率)
   ```
   Debug the code under test to understand the call pattern and adjust the expected count or the code logic.
   ```
2. **** (80% 成功率)
   ```
   Use mock.call_count to get the actual count and assert conditionally: self.assertEqual(mock.call_count, 3)
   ```

## 无效尝试

- **** — Adding extra mock calls to meet the expected count may pass the test but does not reflect real behavior. (60% 失败率)
- **** — Using assert_any_call instead of assert_called_with may pass if the call exists but does not verify the exact number of calls. (50% 失败率)
