# AttributeError: 'dict' 对象没有属性 'items'

- **ID:** `python/pytest-mock-patch-dict`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

字典被 Mock 替换，但代码期望一个真实的字典并调用了 .items() 等字典方法。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   patch.dict('module.my_dict', {'key': 'value'}) or use a simple class that implements the required methods.
   ```
2. **** (85% 成功率)
   ```
   mock_dict = Mock()
mock_dict.items.return_value = [('key', 'value')]
patch('module.my_dict', mock_dict)
   ```

## 无效尝试

- **** — MagicMock supports magic methods but not dict-specific methods like .items() unless configured. (70% 失败率)
- **** — This defeats the purpose of the test and may cause other failures. (90% 失败率)
