nodejs-shizhan
nodejs-shizhan copied to clipboard
62 页 与 63 页
修改好的路由代码,和测试用例代码,在下面,直接测试没法测试 redirect 后的 session。
作者你好,这里修订后 测试用例跑不通了。
https://github.com/MiYogurt/egg-msg-flash/blob/f3c61cc69440684adac2100a7b433fddd1b73884/test/msg-flash.test.js#L25-L59
$ npm run cov
> [email protected] cov /Users/Admin/Desktop/egg-msg-flash-master
> egg-bin cov
test/msg-flash.test.js
✓ should GET /
1) should GET /session1
2) should GET /session3
1 passing (876ms)
2 failing
1) test/msg-flash.test.js
should GET /session1:
Error: expected '{"type":"error","message":{"ss":"some error"}}' response body, got '{}'
at process._tickCallback (internal/process/next_tick.js:63:19)
[use `--full-trace` to display the full stack trace]
2) test/msg-flash.test.js
should GET /session3:
Error: expected '{"type":"warning","message":{"field":{"name":"required"}}}' response body, got '{}'
at process._tickCallback (internal/process/next_tick.js:63:19)
[use `--full-trace` to display the full stack trace]
$ npm run test
> [email protected] test C:\Users\雨果\Desktop\egg-msg-flash
> npm run lint -- --fix && egg-bin pkgfiles && npm run test-local
cd
> [email protected] lint C:\Users\雨果\Desktop\egg-msg-flash
> eslint . "--fix"
> [email protected] test-local C:\Users\雨果\Desktop\egg-msg-flash
> egg-bin test
test/msg-flash.test.js
(node:12432) [DEP0106] DeprecationWarning: crypto.createCipher is deprecated.
√ should GET /
√ should GET /session1
√ should GET /session3
3 passing (3s)
我 clone 下来是可以正常跑通的,你可以自己对照下代码
clone 的那个仓库还没添加 async await 😂
测试好像没法这样去测试,这样测试要黑河测试,白盒可以换一种方式。代码推上去了。
修改图书中的示例代码
router.js
router.get('/session1', async (ctx, next) => {
ctx.flash_error({
ss: 'some error'
})
ctx.body = ctx.session.flash
})
router.get('/session3', async (ctx, next) => {
ctx.flash = {
type: 'warning',
message: {
field: {
name: 'required'
}
}
}
ctx.request.flash('warning', {
field: {
name: 'required'
}
})
ctx.body = ctx.session.flash
})
msg-flash.test.js
it('should GET /', () => {
return app
.httpRequest()
.get('/')
.expect('hi, flash')
.expect(200)
})
it('should GET /session1', () => {
const obj = {
type: 'error',
message: { ss: 'some error' }
}
return app
.httpRequest()
.get('/session1')
.expect(
JSON.stringify(obj)
)
})
it('should GET /session3', () => {
const obj = {
type: 'warning',
message: {
field: {
name: 'required'
}
}
}
return app
.httpRequest()
.get('/session3')
.expect(
JSON.stringify(obj)
)
})
@ifyour 用这个用例去测试,ctx.flash 是下一次请求的时候才能拿得到,第一次 用 ctx.session 里面去拿,这样测试就可以了。
好的,感谢解答疑惑。
去掉这一句话。