# TypeError: 'MagicMock' 类型的对象没有 len()
在处理上述异常时，又发生了一个异常:
TypeError: 不支持 'MagicMock' 与 'MagicMock' 之间的 + 运算

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

## 根因

在需要 MagicMock 的地方使用了普通的 mock.Mock；只有 MagicMock 支持 __len__、__add__、__iter__ 等魔术方法。

## 版本兼容性

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

## 解决方案

1. **** (96% 成功率)
   ```
   from unittest.mock import MagicMock, patch

with patch('myapp.container', new=MagicMock()) as m:
    m.__len__.return_value = 3
    assert len(container) == 3
   ```
2. **** (93% 成功率)
   ```
   with patch('myapp.container', autospec=True) as m:
    m.__len__.return_value = 3
    assert len(container) == 3
   ```

## 无效尝试

- **** — Works for one magic method but breaks as soon as another (e.g., __iter__) is used; the mock is still not a full MagicMock. (80% 失败率)
- **** — configure_mock only sets named attributes; it does not add magic method support to a plain Mock. (90% 失败率)
