Files
vibe_coding/.cursor/skills/vue-testing/references/mock-patterns.md
2026-03-05 21:27:11 +08:00

32 lines
638 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Mock Patterns Quick Reference
## API Mock
```js
import { vi } from 'vitest'
import * as userApi from '@/api/user'
vi.spyOn(userApi, 'getUser').mockResolvedValue({ id: 1, name: 'demo' })
```
## Router Mock
```js
const push = vi.fn()
vi.mock('vue-router', () => ({
useRouter: () => ({ push }),
useRoute: () => ({ params: { id: '1' } })
}))
```
## Pinia
- 默认优先真实 store`setActivePinia(createPinia())`
- 仅在外部依赖复杂且非本次关注点时 mock action
## 禁止过度 Mock
- 不要 mock Vue 内置行为
- 不要 mock 被测模块本身
- 避免因为 mock 过多导致测试与真实行为偏离