{
  "id": "python/pytest-mock-mocker-scope",
  "signature": "RuntimeError: Mock object has no attribute 'assert_called_once_with' after test body? No — actually: AttributeError: 'function' object has no attribute 'assert_called_once'",
  "signature_zh": "AttributeError: 'function' 对象没有属性 'assert_called_once'",
  "regex": "RuntimeError:\\ Mock\\ object\\ has\\ no\\ attribute\\ 'assert_called_once_with'\\ after\\ test\\ body\\?\\ No\\ —\\ actually:\\ AttributeError:\\ 'function'\\ object\\ has\\ no\\ attribute\\ 'assert_called_once'",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "The mocker.patch target replaced the wrong object or the patch was applied after the code under test captured a reference, so the assertion runs against the real function, not a Mock.",
  "root_cause_type": "generic",
  "root_cause_zh": "mocker.patch 替换了错误的对象，或在被测代码捕获引用之后才应用补丁，导致断言针对真实函数而非 Mock 对象。",
  "versions": [
    {
      "version": "7.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The real function has no such method; you must assert on the returned mock object.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Patches a different attribute path, so the code under test still uses the real function.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.93,
      "how": "def test_send(mocker):\n    m = mocker.patch('app.mailer.smtplib.SMTP')\n    send_email('a@b.c')\n    m.assert_called_once()\n    m.return_value.sendmail.assert_called_once()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "def test_send(mocker):\n    m = mocker.patch('app.mailer.smtplib.SMTP')\n    from app.mailer import send_email  # import after patch\n    send_email('a@b.c')\n    m.assert_called_once()",
      "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.83,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-09-17",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}