# AttributeError: <class 'myapp.models.User'> 没有属性 'save'

- **ID:** `python/unittest-mock-patch-object-not-found`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

patch.object 用于修补目标对象上不存在的属性。如果方法是继承的或动态定义的，就会发生这种情况。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   with patch.object(User, 'save', create=True) as mock_save:
    ...
   ```
2. **** (85% 成功率)
   ```
   If save is inherited from BaseModel, patch that: patch.object(BaseModel, 'save')
   ```

## 无效尝试

- **** — patch also requires the attribute to exist unless create=True is used. (70% 失败率)
- **** — This changes production code to satisfy a test, which is not ideal. (80% 失败率)
