{
  "id": "python/pytest-mock-not-found",
  "signature": "AttributeError: 'function' object has no attribute 'assert_called_once'",
  "signature_zh": "AttributeError: 'function' 对象没有属性 'assert_called_once'",
  "regex": "AttributeError:\\ 'function'\\ object\\ has\\ no\\ attribute\\ 'assert_called_once'",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "The test attempts to use mock assertion methods on a regular function instead of a Mock object. This happens when a function is patched incorrectly (e.g., using @patch without creating a Mock) or when the mock is not properly injected.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试尝试在普通函数上使用mock断言方法，而不是在Mock对象上。这通常发生在函数被错误地打补丁时（例如，使用 @patch 但没有创建 Mock），或者mock没有被正确注入。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Functions do not have mock assertion methods; this is a fundamental misunderstanding of the mock API.",
      "fail_rate": 0.98,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Creating a Mock object does not replace the real function; the test still calls the original function.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "mocker.patch requires a target string; omitting it raises a TypeError or patches the wrong object.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "from unittest.mock import patch\n\n@patch('mymodule.my_function')\ndef test_my_function(mock_func):\n    my_function()\n    mock_func.assert_called_once()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "def test_my_function(mocker):\n    mock_func = mocker.patch('mymodule.my_function')\n    my_function()\n    mock_func.assert_called_once()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "If you have a reference to a Mock, ensure you're calling the mock, not the original:\n\nmock = Mock()\nmymodule.my_function = mock\nmymodule.my_function()\nmock.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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-08-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}