{
  "id": "python/unittest-mock-method-not-called",
  "signature": "AssertionError: Expected 'send_email' to have been called once. Called 0 times.",
  "signature_zh": "AssertionError: 预期 'send_email' 被调用一次。实际调用 0 次。",
  "regex": "AssertionError:\\ Expected\\ 'send_email'\\ to\\ have\\ been\\ called\\ once\\.\\ Called\\ 0\\ times\\.",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "The function under test calls a name that was imported into its own module (`from myapp.mail import send_email`), but the test patched `myapp.mail.send_email`. The local binding still points to the original function.",
  "root_cause_type": "generic",
  "root_cause_zh": "被测函数调用的是导入到其自身模块中的名称（`from myapp.mail import send_email`），但测试补丁了 `myapp.mail.send_email`。本地绑定仍指向原始函数。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The mock was never invoked; changing args doesn't help.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Works only if you patch the correct binding; patching the definition module alone is a no-op for the caller.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "from unittest import mock\n\n# services.py does: from myapp.mail import send_email\ndef test_signup():\n    with mock.patch(\"myapp.services.send_email\") as fake_send:\n        signup(\"a@b.c\")\n        fake_send.assert_called_once_with(\"a@b.c\")",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "# services.py\nimport myapp.mail as mail\n\ndef signup(email):\n    mail.send_email(email)\n\n# test\nwith mock.patch(\"myapp.mail.send_email\") as fake_send:\n    signup(\"a@b.c\")",
      "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.89,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-29",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}