初始化
This commit is contained in:
31
.cursor/skills/vue-testing/references/mock-patterns.md
Normal file
31
.cursor/skills/vue-testing/references/mock-patterns.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# 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 过多导致测试与真实行为偏离
|
||||
Reference in New Issue
Block a user