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

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

## 根因

裸 `Mock()` 对任意属性访问都会返回子 mock，但带 `spec=` 的 `MagicMock`/`Mock` 会限制属性。若测试使用了 `spec=`，而代码调用了 spec 中不存在的方法，就会出现此错误。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Add the missing method to the spec's source class, or use `spec=` on the real class:

from unittest.mock import MagicMock
from mylib import Client
m = MagicMock(spec=Client)
# ensure Client actually defines .send()
   ```
2. **** (90% 成功率)
   ```
   Use `autospec=True` in patch so the mock mirrors the real signature:

@patch('mymod.Client', autospec=True)
def test_client(MockClient):
    ...
   ```

## 无效尝试

- **** — Disables safety checks; typos in method calls are no longer caught and tests may pass silently. (60% 失败率)
- **** — With `spec_set=True` you cannot add new attributes; with plain `spec` this works but defeats the purpose. (50% 失败率)
