一键导入
pytest-asyncio-httpx-mocking
// When masking httpx.AsyncClient with unittest.mock in Pytest, AsyncMock must be used instead of MagicMock for async methods like post/get to prevent TypeError when awaited.
// When masking httpx.AsyncClient with unittest.mock in Pytest, AsyncMock must be used instead of MagicMock for async methods like post/get to prevent TypeError when awaited.
| name | pytest-asyncio-httpx-mocking |
| description | When masking httpx.AsyncClient with unittest.mock in Pytest, AsyncMock must be used instead of MagicMock for async methods like post/get to prevent TypeError when awaited. |
patch.object(httpx.AsyncClient, 'post', return_value=mock_response)。response = await client.post(...) 的地方抛出 TypeError: object MagicMock can't be used in 'await' expression。httpx.AsyncClient.post 是一个 async def 方法,调用它会返回一个可等待(awaitable)的协程。patch 或 MagicMock 没有自动推断对象的异步特性时,它只是同步地返回了 return_value。当事件循环试图 await 这个同步的 MagicMock 对象时,就会报错。patch 参数里显式使用 new=AsyncMock(return_value=...) 或 new_callable=AsyncMock。❌ 错误写法:
from unittest.mock import patch, MagicMock
mock_response = MagicMock(status_code=200)
# 当被 await 时会触发 TypeError!
with patch.object(httpx.AsyncClient, 'post', return_value=mock_response):
await my_crawler.fetch()
✅ 正确写法:
from unittest.mock import patch, MagicMock, AsyncMock
mock_response = MagicMock(status_code=200) # Response 对象本身及其方法通常是同步的
# 正确!覆盖掉原来的方法,使其行为成为一个 AsyncMock
with patch.object(httpx.AsyncClient, 'post', new=AsyncMock(return_value=mock_response)):
await my_crawler.fetch()
使用 side_effect 模拟循序多次请求:
with patch.object(httpx.AsyncClient, 'get', new=AsyncMock(side_effect=[mock_1, mock_2])):
...
async def 的 Mock,必须保证它被调用时能走协程语境。httpx.AsyncClient.get 是异步的(需 AsyncMock),但它返回的 Response 对象上的 .json() 是同步的(需 MagicMock 即可)。i18n (internationalization) toolkit for projects using i18next. Provides three main functions: (1) i18n-check - Detect hardcoded Chinese text in HTML/JS files, (2) i18n-fix - Replace hardcoded text with i18n markers, (3) i18n-sync - Align translation keys across multiple languages (zh-CN, zh-TW, en, ja, ko, ru, es, pt). Use when working on internationalization tasks, detecting untranslated strings, or syncing locale files.
Gemini 模型通过 OpenAI 兼容 API 接入指南。包含:(1) 辅助 API 配置(summary/correction/emotion/vision),(2) extra_body 格式用于控制 thinking,(3) 响应格式处理(markdown 代码块)。当需要将 Gemini 作为辅助模型接入、配置 thinking 参数、或处理 Gemini API 返回格式时使用。
应对 WebGL 端 MMD 物理 (Ammo.js) 炸模、穿模、卡顿等极限边缘工况的终极调优指南与 Hack 技巧。
Best practices for extracting data from modern React/Vue SSR pages (like Next.js or Nuxt.js) by targeting hydration state blocks (__NEXT_DATA__, __NUXT__) using regex and `jmespath`, avoiding brittle DOM selector scraping.
Convention for reporting errors from multiprocessing TTS workers to the main process frontend. Use this skill when modifying, adding, or debugging TTS workers in tts_client.py to ensure connection errors, quotas, and API limits correctly display Toast notifications to the user rather than failing silently.
Dealing with delayed DOM generation, lazy loading, and optimistic state synchronization in vanilla JavaScript without a reactive framework.