# TypeError: test_x() 缺少 1 个必需的位置参数: 'mock_b'
处理上述异常时又出现另一个异常:
TypeError: test_x() 接受 1 个位置参数但传入了 3 个

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

## 根因

堆叠的 @patch 装饰器按自下而上的顺序注入 mock，但测试函数参数按自上而下（或相反）编写。这导致 mock 与参数对应关系错位。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Remember: decorators apply bottom-up, so the LAST decorator is the FIRST parameter:
  @patch('a.foo')
  @patch('a.bar')
  def test_x(mock_bar, mock_foo): ...
   ```
2. **** (93% 成功率)
   ```
   Use context managers instead of decorators to avoid ordering confusion:
  def test_x():
      with patch('a.foo') as mf, patch('a.bar') as mb:
          ...
   ```

## 无效尝试

- **** — unittest passes mocks positionally; *args accepts them but the names inside the body no longer refer to the intended mocks. (85% 失败率)
- **** — Non-deterministic across refactors; masks the real order rule. (60% 失败率)
