# AttributeError: Mock 对象没有属性 'fetch_user'

- **ID:** `python/unittest-mock-autospec-missing`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用了普通 MagicMock 而非 create_autospec，方法名拼写错误被静默接受，直到 mock 被当作真实对象使用并查找属性时才暴露。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   from unittest.mock import create_autospec
mock_svc = create_autospec(UserService, instance=True)
# mock_svc.fetch_user now exists and typo'd names raise AttributeError
   ```
2. **** (90% 成功率)
   ```
   from unittest.mock import Mock
mock_svc = Mock(spec=UserService)
   ```

## 无效尝试

- **** — Fixes one symptom but the mock still accepts any typo, so the next misspelling fails the same way. (70% 失败率)
- **** — Loses the ability to assert calls and may trigger real I/O or DB access. (75% 失败率)
