# TypeError: 不支持的操作数类型 +: 'Mock' 和 'int'

- **ID:** `python/unittest-mock-magic-method-not-supported`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

普通的 unittest.mock.Mock 不实现 __add__、__len__、__iter__ 等魔法方法。当被测代码依赖 dunder 方法时，应使用 MagicMock（或 AsyncMock）。

## 版本兼容性

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

## 解决方案

1. **** (98% 成功率)
   ```
   from unittest.mock import MagicMock
m = MagicMock()
m.__add__.return_value = 42
assert m + 1 == 42
   ```
2. **** (90% 成功率)
   ```
   from unittest.mock import AsyncMock
m = AsyncMock()
m.__aenter__.return_value = resource
   ```

## 无效尝试

- **** — Plain Mock ignores magic method assignments; you must use MagicMock for them to take effect. (90% 失败率)
- **** — int(Mock()) raises TypeError because Mock has no __int__; converting does not help. (95% 失败率)
