Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 734854c

Browse files
authored
docs(unittest): add bootstrap usage (eggjs#1278)
1 parent ebbbcd5 commit 734854c

1 file changed

Lines changed: 41 additions & 34 deletions

File tree

docs/source/zh-cn/core/unittest.md

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ API 升级,测试用例可以很好地检查代码是否向下兼容。
3939

4040
> Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.
4141
42-
加上 [thunk-mocha](https://npmjs.com/thunk-mocha) 模块的帮助,
42+
加上 [co-mocha](https://npmjs.com/co-mocha) 模块的帮助,
4343
扩展了 Mocha 的多种用例书写方式,例如 generator function,async await 等。
4444

4545
### AVA
@@ -103,7 +103,7 @@ test
103103
### 测试运行工具
104104

105105
统一使用 [egg-bin 来运行测试脚本](./development.md#单元测试)
106-
自动将内置的 Mocha、thunk-mocha、power-assert,istanbul 等模块组合引入到测试脚本中,
106+
自动将内置的 Mocha、co-mocha、power-assert,istanbul 等模块组合引入到测试脚本中,
107107
让我们**聚焦精力在编写测试代码**上,而不是纠结选择那些测试周边工具和模块。
108108

109109
只需要在 `package.json` 上配置好 `scripts.test` 即可。
@@ -170,6 +170,17 @@ describe('test/controller/home.test.js', () => {
170170
这样我们就拿到了一个 app 的引用,接下来所有测试用例都会基于这个 app 进行。
171171
更多关于创建 app 的信息请查看 [`mock.app(options)`](https://github.com/eggjs/egg-mock#options) 文档。
172172

173+
每一个测试文件都需要这样创建一个 app 实例非常冗余,因此 egg-mock 提供了一个 bootstrap 文件,可以直接从它上面拿到我们所常用的实例:
174+
175+
```js
176+
// test/controller/home.test.js
177+
const { app, mock, assert } = require('egg/mock/bootstrap');
178+
179+
describe('test/controller/home.test.js', () => {
180+
// test cases
181+
});
182+
```
183+
173184
### ctx
174185

175186
我们除了 app,还需要一种方式便捷地拿到 ctx,方便我们进行 Extend、Service、Helper 等测试。
@@ -207,9 +218,11 @@ it('should mock ctx.user', () => {
207218

208219
```js
209220
// Bad
210-
const mock = require('egg-mock');
221+
const { app } = require('egg-mock/bootstrap');
222+
211223
describe('bad test', () => {
212-
const app = mock.app();
224+
doSomethingBefore();
225+
213226
it('should redirect', () => {
214227
return app.httpRequest()
215228
.get('/')
@@ -219,20 +232,18 @@ describe('bad test', () => {
219232
```
220233

221234
Mocha 刚开始运行的时候会载入所有用例,这时 describe 方法就会被调用,
222-
`mock.app()` 就会启动。
235+
`doSomethingBefore` 就会启动。
223236
如果希望使用 only 的方式只执行某个用例那段代码还是会被执行,这是非预期的。
224237

225238
正确的做法是将其放到 before 中,只有运行这个套件中某个用例才会执行。
226239

227240
```js
228241
// Good
229-
const mock = require('egg-mock');
242+
const { app } = require('egg-mock/bootstrap');
243+
230244
describe('good test', () => {
231-
let app;
232-
before(() => {
233-
app = mock.app();
234-
return app.ready();
235-
});
245+
before(() => doSomethingBefore());
246+
236247
it('should redirect', () => {
237248
return app.httpRequest()
238249
.get('/')
@@ -257,29 +268,32 @@ describe('egg test', () => {
257268

258269
## 异步测试
259270

260-
egg-bin 会自动加载 thunk-mocha 插件测试异步调用,它支持多种写法,比如上面启动完成,`app.ready()` 返回一个 Promise
271+
egg-bin 会自动加载 co-mocha 插件测试异步调用,它支持多种写法,比如上面 `app.httpRequest` 方法支持返回 Promise
261272

262273
```js
263-
// 使用 callback 的方式
264-
before(done => {
265-
const app = mm.app();
266-
app.ready(done);
274+
// 使用返回 Promise 的方式
275+
it('should redirect', () => {
276+
return app.httpRequest()
277+
.get('/')
278+
.expect(302);
267279
});
268280

269-
// 使用 Promise
270-
before(() => {
271-
const app = mm.app();
272-
return app.ready();
281+
// 使用 callback 的方式
282+
it('should redirect', done => {
283+
app.httpRequest()
284+
.get('/')
285+
.expect(302, done);
273286
});
274287

275288
// 使用 generator
276-
before(function* () {
277-
const app = mm.app();
278-
yield app.ready();
289+
it('should redirect', function* () {
290+
yield app.httpRequest()
291+
.get('/')
292+
.expect(302);
279293
});
280294
```
281295

282-
使用哪种写法取决于不同应用场景,如果遇到多个异步可以使用 generator function,也可以拆分成多个 before
296+
使用哪种写法取决于不同应用场景,如果遇到多个异步可以使用 generator function,也可以拆分成多个测试用例
283297

284298
## Controller 测试
285299

@@ -305,18 +319,9 @@ exports.index = function* (ctx) {
305319
写一个完整的单元测试,它的测试代码 `test/controller/home.test.js` 如下:
306320

307321
```js
308-
const assert = require('assert');
309-
const mock = require('egg-mock');
322+
const { app, mock, assert } = require('egg-mock/bootstrap');
310323

311324
describe('test/controller/home.test.js', () => {
312-
let app;
313-
before(() => {
314-
// 创建当前应用的 app 实例
315-
app = mock.app();
316-
// 等待 app 启动成功,才能执行测试用例
317-
return app.ready();
318-
});
319-
320325
describe('GET /', () => {
321326
it('should status 200 and get the body', () => {
322327
// 对 app 发起 `GET /` 请求
@@ -672,6 +677,8 @@ describe('some tes', () => {
672677
});
673678
```
674679

680+
**引入 `egg-mock/bootstrap` 时,会自动在 `afterEach` 钩子中还原所有的 mock,不需要在测试文件中再次编写。**
681+
675682
下面会详细解释一下 egg-mock 的常见使用场景。
676683

677684
### Mock 属性和方法

0 commit comments

Comments
 (0)