python runtime_error ai_generated true

断言错误:期望 mock 被调用一次。实际调用 0 次。

AssertionError: Expected mock to have been called once. Called 0 times.

ID: python/unittest-mock-return-value-not-asserted

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-02-14首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

mock 对象的调用次数与预期不符,通常是由于补丁目标错误或逻辑错误。

English

The mock object's call count doesn't match the expected number, often due to incorrect patch target or logic.

generic

解决方案

  1. 90% 成功率
    with patch('module.function') as mock_func:
        call_code()
        mock_func.assert_called_once()
  2. 85% 成功率
    mock_func.assert_any_call(arg1, arg2)

无效尝试

常见但无效的做法:

  1. 70% 失败

    This masks the real problem of why the code isn't calling the mock.

  2. 90% 失败

    The test becomes ineffective.