python runtime_error ai_generated true

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

ID: python/unittest-mock-call-count

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-08-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active
3.10 active

Root Cause

使用assert_called_once_with或assert_called_once时,mock没有被调用或调用次数不符。

generic

中文

使用assert_called_once_with或assert_called_once时,mock没有被调用或调用次数不符。

Workarounds

  1. 90% success
    print(mock.call_count)
    print(mock.mock_calls)
  2. 85% success
    mock.assert_any_call(args)

Dead Ends

Common approaches that don't work:

  1. 60% fail

    return_value是返回值,不是调用记录

  2. 70% fail

    如果代码逻辑错误,增加调用次数是错误修复