# httpx.ConnectError: [Errno -2] 无法解析名称或服务

- **ID:** `python/pytest-httpx-mock-interface-mismatch`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

respx/httpx mock 路由未注册所请求的 URL，或客户端实例在 mock 安装之前创建，导致尝试真实网络调用。

## 版本兼容性

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

## 解决方案

1. **** (93% 成功率)
   ```
   import httpx, respx

@respx.mock
def test_get():
    respx.get('https://api.example.com/users/1').mock(
        return_value=httpx.Response(200, json={'id': 1})
    )
    with httpx.Client() as c:
        r = c.get('https://api.example.com/users/1')
    assert r.json()['id'] == 1
   ```
2. **** (90% 成功率)
   ```
   with respx.mock(assert_all_called=True) as router:
    router.get('https://api.example.com/').mock(return_value=httpx.Response(200))
    run_code()
   ```

## 无效尝试

- **** — Over-broad; hides typos in URLs and returns the same mock for every request, defeating per-endpoint assertions. (70% 失败率)
- **** — Bypasses proxies but doesn't prevent the real DNS lookup; the error persists. (80% 失败率)
