# 属性错误：Mock 对象没有属性 'some_method'

- **ID:** `python/attributeerror-mock-object-has-no-attribute`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

创建模拟对象时未指定属性，代码尝试访问未定义的属性。

## 版本兼容性

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

## 解决方案

1. **Configure mock with spec or autospec** (95% 成功率)
   ```
   mock = Mock(spec=MyClass); mock.some_method.return_value = 42
   ```
2. **Set attribute directly on mock** (90% 成功率)
   ```
   mock = Mock(); mock.some_method = MagicMock(return_value=42)
   ```

## 无效尝试

- **Adding attribute via mock.attach_mock** — attach_mock is for attaching additional mocks, not simple attributes. (60% 失败率)
- **Using mock.reset_mock()** — Reset clears call history but does not add missing attributes. (80% 失败率)
