python
runtime_error
ai_generated
true
断言错误:预期模拟被调用 3 次,实际被调用 2 次。
AssertionError: Expected mock to have been called 3 times. Called 2 times.
ID: python/unittest-mock-call-count-assertion
80%修复率
87%置信度
0证据数
2026-10-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.9 | active | — | — | — |
根因分析
测试断言了 mock.assert_called_with 或 mock.assert_called_once,但实际调用次数与预期不符,通常是由于被测试代码中的条件逻辑。
English
The test asserts mock.assert_called_with or mock.assert_called_once, but the actual number of calls does not match the expectation, often due to conditional logic in the code under test.
解决方案
-
85% 成功率
Debug the code under test to understand the call pattern and adjust the expected count or the code logic.
-
80% 成功率
Use mock.call_count to get the actual count and assert conditionally: self.assertEqual(mock.call_count, 3)
无效尝试
常见但无效的做法:
-
60% 失败
Adding extra mock calls to meet the expected count may pass the test but does not reflect real behavior.
-
50% 失败
Using assert_any_call instead of assert_called_with may pass if the call exists but does not verify the exact number of calls.