# AssertionError: 未找到预期调用。
预期: send(1)
  实际: 未被调用。

- **ID:** `python/pytest-mock-mocker-scope-mismatch`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

mocker.patch 在函数作用域的 fixture 中应用，但被 patch 的调用发生在会话作用域 setup 中，该 setup 在 patch 安装之前已执行。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.14.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   @pytest.fixture(scope='session')
def patched_send():
    with mock.patch('myapp.notify.send') as m:
        yield m

def test_x(patched_send):
    run_setup()
    patched_send.assert_called_with(1)
   ```
2. **** (93% 成功率)
   ```
   def test_x(mocker):
    m = mocker.patch('myapp.notify.send')
    run_setup()  # called here, not in a fixture
    m.assert_called_with(1)
   ```

## 无效尝试

- **** — The mock was never called in the first place; resetting does not retroactively capture calls that happened before patching. (90% 失败率)
- **** — create=True only bypasses AttributeError; the timing problem (patch applied after the call) is unchanged. (85% 失败率)
