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

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

## 根因

使用 `create_autospec` 将 mock 限制为真实对象的属性；如果 `some_method` 是动态添加的或拼写错误，访问会失败。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   class MyClass:
    def some_method(self): ...

mock = create_autospec(MyClass)
mock.some_method()
   ```
2. **** (80% 成功率)
   ```
   mock = Mock(spec_set=['some_method', 'other_method'])
   ```

## 无效尝试

- **** — Loses the safety of autospec and may hide typos in method names. (60% 失败率)
- **** — Defeats the purpose of autospec and may not match the real signature. (70% 失败率)
