# AttributeError: 'function' 对象没有属性 'assert_called_once'

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

## 根因

mocker.patch 替换了错误的对象，或在被测代码捕获引用之后才应用补丁，导致断言针对真实函数而非 Mock 对象。

## 版本兼容性

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

## 解决方案

1. **** (93% 成功率)
   ```
   def test_send(mocker):
    m = mocker.patch('app.mailer.smtplib.SMTP')
    send_email('a@b.c')
    m.assert_called_once()
    m.return_value.sendmail.assert_called_once()
   ```
2. **** (85% 成功率)
   ```
   def test_send(mocker):
    m = mocker.patch('app.mailer.smtplib.SMTP')
    from app.mailer import send_email  # import after patch
    send_email('a@b.c')
    m.assert_called_once()
   ```

## 无效尝试

- **** — The real function has no such method; you must assert on the returned mock object. (90% 失败率)
- **** — Patches a different attribute path, so the code under test still uses the real function. (80% 失败率)
