{
  "id": "python/pytest-mock-mocker-scope-mismatch",
  "signature": "AssertionError: expected call not found.\nExpected: send(1)\n  Actual: not called.",
  "signature_zh": "AssertionError: 未找到预期调用。\n预期: send(1)\n  实际: 未被调用。",
  "regex": "AssertionError:\\ expected\\ call\\ not\\ found\\.\\\nExpected:\\ send\\(1\\)\\\n\\ \\ Actual:\\ not\\ called\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "mocker.patch was applied inside a fixture with function scope, but the patched call happens in a session-scoped setup that ran before the patch was installed.",
  "root_cause_type": "generic",
  "root_cause_zh": "mocker.patch 在函数作用域的 fixture 中应用，但被 patch 的调用发生在会话作用域 setup 中，该 setup 在 patch 安装之前已执行。",
  "versions": [
    {
      "version": "3.14.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The mock was never called in the first place; resetting does not retroactively capture calls that happened before patching.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "create=True only bypasses AttributeError; the timing problem (patch applied after the call) is unchanged.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "@pytest.fixture(scope='session')\ndef patched_send():\n    with mock.patch('myapp.notify.send') as m:\n        yield m\n\ndef test_x(patched_send):\n    run_setup()\n    patched_send.assert_called_with(1)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "def test_x(mocker):\n    m = mocker.patch('myapp.notify.send')\n    run_setup()  # called here, not in a fixture\n    m.assert_called_with(1)",
      "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-07-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}