{
  "id": "python/unittest-mock-assertion-failure",
  "signature": "AssertionError: Expected 'send_email' to be called once. Called 0 times.",
  "signature_zh": "AssertionError: 期望 'send_email' 被调用一次。实际调用 0 次。",
  "regex": "AssertionError:\\ Expected\\ 'send_email'\\ to\\ be\\ called\\ once\\.\\ Called\\ 0\\ times\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A mock's assert_called_once() failed because the patched function was never invoked, usually due to patching the wrong module reference, an early return in the code under test, or the call happening in a different thread.",
  "root_cause_type": "generic",
  "root_cause_zh": "mock 的 assert_called_once() 失败，因为被补丁的函数从未被调用，通常由于补丁指向了错误的模块引用、被测代码提前返回或调用发生在不同线程。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This only checks that it was called at least once, but the count is 0, so it still fails.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "create=True creates a mock attribute but does not make the real code call it.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.92,
      "how": "Patch the name where it is used and assert against the same object:\n\nwith patch('app.orders.send_email') as m:\n    place_order(order)\n    m.assert_called_once_with('user@example.com')",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "Debug by printing the mock's call list:\n\nprint(m.mock_calls)\nprint(m.call_args_list)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-04-18",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}