{
  "id": "python/unittest-mock-autospec-missing-attr",
  "signature": "AttributeError: Mock object has no attribute 'send'",
  "signature_zh": "AttributeError: Mock 对象没有属性 'send'",
  "regex": "AttributeError:\\ Mock\\ object\\ has\\ no\\ attribute\\ 'send'",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A bare `Mock()` returns child mocks for any attribute access, but `MagicMock`/`Mock` with `spec=` restricts attributes. If a test uses `spec=` and the code calls a method not in the spec, this error appears.",
  "root_cause_type": "generic",
  "root_cause_zh": "裸 `Mock()` 对任意属性访问都会返回子 mock，但带 `spec=` 的 `MagicMock`/`Mock` 会限制属性。若测试使用了 `spec=`，而代码调用了 spec 中不存在的方法，就会出现此错误。",
  "versions": [
    {
      "version": "3.10",
      "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": "Disables safety checks; typos in method calls are no longer caught and tests may pass silently.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "With `spec_set=True` you cannot add new attributes; with plain `spec` this works but defeats the purpose.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Add the missing method to the spec's source class, or use `spec=` on the real class:\n\nfrom unittest.mock import MagicMock\nfrom mylib import Client\nm = MagicMock(spec=Client)\n# ensure Client actually defines .send()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use `autospec=True` in patch so the mock mirrors the real signature:\n\n@patch('mymod.Client', autospec=True)\ndef test_client(MockClient):\n    ...",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-09-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}