Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shareLog
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontEnd
shareLog
Commits
316e5663
Commit
316e5663
authored
Jan 10, 2019
by
member
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update 2.vue单元测试和组件通信(石瑶).md
parent
e2f5a696
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
1 deletions
+112
-1
2.vue单元测试和组件通信(石瑶).md
2.vue单元测试和组件通信(石瑶).md
+112
-1
No files found.
2.vue单元测试和组件通信(石瑶).md
View file @
316e5663
-- "a/2.vue\345\215\225\345\205\203\346\265\213\350\257\225\345\222\214\347\273\204\344\273\266\351\200\232\344\277\241(\347\237\263\347\221\266).md"
### 主题
### 主题
Vue单元测试和组件通信
#### 单元测试
基于 vue-cli 3.0 的脚手架,然后 vue create name 创建一个文件名。推荐使用自己配置
测试框架选 mocha + chai 断言库 jest(我没有用过) 不需要手动引入断言库。
```
.
├── build // webpack配置文件
├── src // 源码目录
│ ├── components // 组件
│
│── test
│ ├── specs // 测试脚本目录
│ | ├── HelloWorld.spec.js // 测试脚本 需要将测试代码放在这里
| | |
| | |
├── index.html // 入口html文件
.
```
#### 测试脚本代码
假设有这样的Vue代码
```
<template>
<div>{{msg}}</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msg: String
},
data () {
return {
name: 'name'
}
},
methods: {
setName(name) {
this.name = name;
}
}
}
</script>
```
测试脚本里面包含一个或多个describe块 称为测试套件
```
import Vue from 'vue';
import HelloWorld from '@/component/HelloWorld.vue';
// data 属性
describe('HelloWorld.vue', () => {
const Cotr = Vue.extend(HelloWorld);
// 一个测试套件里面包含一个或多个it块 称为测试用例
it('renders props.msg when passed', () => {
const vm = new Cotr().$mount();
expect(vm.name).to.equal('name');
});
// methods 方法
it('给setMessage方法设置参数', () => {
vm.setName('张三')
expect(vm.name).to.equal('张三')
});
it('测试data数据', () => {
const Cotr = Vue.extend(HelloWorld)
const propData = { msg: 'hello' }
const vm = new Cotr({ propsData: propData }).$mount()
expect(vm.$el.textContent).to.equal('hello')
})
// 异步更新
it('异步更新', done => {
const vm = new Cotr().$mount();
vm.msg = 'hello vue'
Vue.nextTick(_ => {
expect(vm.$el.textContent).to.equal('hello vue')
})
done()
})
});
```
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment