Skip to content

Commit 7ecd54e

Browse files
committed
fix(lesson7): 修复 lesson7 代码示例
- 修改项目空间为 lesson7/vendor - chai.js 断言库直接引用 github 线上版本 - 精简项目结构 fixes alsotang#56
1 parent 81e859f commit 7ecd54e

File tree

7 files changed

+1151
-5219
lines changed

7 files changed

+1151
-5219
lines changed

lesson7/README.md

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
## 目标
44

5-
建立一个 lesson7 项目,在其中编写代码。
5+
建立一个 lesson7 项目,在其中编写代码,我们暂时命名为 *vendor*
6+
根据下面的步骤,最终的项目结构应该长[这样](https://github.com/alsotang/node-lessons/tree/master/lesson7/vendor)
67

7-
main.js: 类似上文提到的 fibonacci 函数
8+
这次我们测试的对象是上文提到的 fibonacci 函数
89

910
此函数的定义为 `int fibonacci(int n)`
1011

1112
* 当 n === 0 时,返回 0;n === 1时,返回 1;
1213
* n > 1 时,返回 `fibonacci(n) === fibonacci(n-1) + fibonacci(n-2)`,如 `fibonacci(10) === 55`;
1314

14-
vendor文件夹: 前端单元测试的环境。
15-
16-
vendor/tests.js 编写针对前端脚本的测试用例
17-
1815
## 知识点
1916

2017
1. 学习使用测试框架 mocha 进行前端测试 : http://mochajs.org/
@@ -33,20 +30,21 @@ vendor/tests.js 编写针对前端脚本的测试用例
3330

3431
#### 浏览器环境执行
3532

36-
我们首先搭建一个测试原型,只需要执行
33+
我们首先搭建一个测试原型,用 mocha 自带的脚手架可以自动生成。
3734

38-
```js
39-
//f2e 是原型生成的目录
40-
mocha init f2e
35+
```shell
36+
cd vendor # 进入我们的项目文件夹
37+
npm i -g mocha # 安装全局的 mocha 命令行工具
38+
mocha init . # 生成脚手架
4139
```
4240

43-
mocha就会自动帮我们生成一个简单的测试原型
41+
mocha就会自动帮我们生成一个简单的测试原型, 目录结构如下
4442
```shell
4543
.
46-
├── index.html
44+
├── index.html # 这是前端单元测试的入口
4745
├── mocha.css
4846
├── mocha.js
49-
└── tests.js
47+
└── tests.js # 我们的单元测试代码将在这里编写
5048
```
5149

5250
其中 index.html 是单元测试的入口,tests.js 是我们的测试用例文件。
@@ -55,7 +53,7 @@ mocha就会自动帮我们生成一个简单的测试原型
5553

5654
```html
5755
<div id="mocha"></div>
58-
<script src='chai.js'></script>
56+
<script src='https://raw.githubusercontent.com/chaijs/chai/master/chai.js'></script>
5957
<script>
6058
var fibonacci = function (n) {
6159
if (n === 0) {
@@ -94,34 +92,44 @@ mocha没有提供一个命令行的前端脚本测试环境(因为我们的脚
9492
npm i -g mocha-phantomjs
9593
```
9694

97-
然后将index.html对应部分修改为
95+
然后在 index.html 的页面下加上这段兼容代码
96+
97+
```html
98+
<script>mocha.run()</script>
99+
```
100+
101+
改为
98102

99103
```html
100104
<script>
101-
if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
102-
else { mocha.run(); }
105+
if (window.mochaPhantomJS) {
106+
mochaPhantomJS.run();
107+
} else {
108+
mocha.run();
109+
}
103110
</script>
104111
```
105112

106-
然后我们在命令行中运行
113+
这时候, 我们在命令行中运行
107114

108115
```shell
109116
mocha-phantomjs index.html
110117
```
111118

112119
结果展现是不是和后端代码测试很类似 :smile:
113120

114-
你也可以直接在package.json中定义
121+
更进一步,我们可以直接在 package.json 的 scripts 中添加
122+
(package.json 通过 `npm init` 生成,这里不再赘述)
115123

116124
```json
117125
"scripts": {
118-
"test": "./node_modules/.bin/mocha-phantomjs vendor/index.html"
126+
"test": "mocha-phantomjs index.html"
119127
},
120128
```
121129

122130
将mocha-phantomjs作为依赖
123131

124-
```bash
132+
```shell
125133
npm i mocha-phantomjs --save-dev
126134
```
127135

@@ -131,4 +139,7 @@ npm i mocha-phantomjs --save-dev
131139
npm test
132140
```
133141

142+
运行结果如下:
143+
134144
至此,我们实现了前端脚本的单元测试,基于 phanatomjs 你几乎可以调用所有的浏览器方法,而 mocha-phanatomjs 也可以很便捷地将测试结果反馈到 mocha,便于后续的持续集成。
145+

lesson7/package.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)