# 属性错误：'function' 对象没有属性 'return_value'

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

## 根因

尝试在常规函数上设置 return_value，而不是在 Mock 对象上，通常是因为 patch 装饰器未正确应用或目标不是模拟对象。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Ensure the patch decorator is applied to the test function: @patch('module.function') def test(mock_func): mock_func.return_value = 42
   ```
2. **** (85% 成功率)
   ```
   Use patch as a context manager: with patch('module.function') as mock_func: mock_func.return_value = 42
   ```

## 无效尝试

- **** — Wrapping the function in a Mock manually may work but bypasses the patch mechanism and can cause test isolation issues. (50% 失败率)
- **** — Using patch.object with a string instead of the actual object may still fail if the attribute does not exist. (60% 失败率)
