# TypeError: 不可哈希的类型: 'dict'

- **ID:** `python/unittest-testcase-assertcountequal-unhashable`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

assertCountEqual 会构建两个序列的 Counter，要求元素可哈希。字典列表因 dict 不可哈希而失败。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   actual = sorted(actual, key=lambda d: d['id'])
expected = sorted(expected, key=lambda d: d['id'])
self.assertEqual(actual, expected)
   ```
2. **** (88% 成功率)
   ```
   from collections import Counter
self.assertEqual(
    Counter(frozenset(d.items()) for d in actual),
    Counter(frozenset(d.items()) for d in expected),
)
   ```

## 无效尝试

- **** — Loses key ordering independence; two equal dicts with different insertion order produce different strings and the test fails spuriously. (70% 失败率)
- **** — assertEqual is order-sensitive and fails when the sequences contain the same items in a different order. (75% 失败率)
