# 属性错误：当在不可调用的模拟对象上设置return_value时，模拟对象没有'return_value'属性

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

## 根因

尝试在创建时spec=None且非可调用的Mock实例上设置return_value，但该mock被当作函数使用。

## 版本兼容性

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

## 解决方案

1. **Ensure the mock is created as a callable by using MagicMock or by passing spec=SomeCallable** (90% 成功率)
   ```
   from unittest.mock import MagicMock
mock = MagicMock()
mock.return_value = 42
result = mock()  # Works
Or: mock = Mock(spec=lambda: None)
mock.return_value = 42
   ```
2. **Use side_effect instead of return_value for non-callable mocks** (0% 成功率)

## 无效尝试

- **Creating a new MagicMock instead of Mock** — MagicMock is callable by default, but the underlying issue may be that the mock is not intended to be callable. (50% 失败率)
- **Setting return_value directly without checking if the mock is callable** — This will still raise an AttributeError if the mock is not callable. (70% 失败率)
