[英]How to carry unit test on router function NodeJS / Mocha / Chai
How should it seems, if I want carry unit tests on this router : 如果要在此路由器上进行单元测试,应该怎么看:
router
.post('/set-permission', (req, res) => {
Board.updateBoard(req.body)
.then(resp => {
console.log(resp.permissions);
res.json(resp.permissions);
})
})
module.exports = router;
I have folder in project/test inside is file .js with required chai library. 我在project / test中有文件夹,里面是带有所需chai库的.js文件。 Now I should execute this function with parameter? 现在我应该使用参数执行此功能? But how? 但是如何? Could someone explain me?? 有人可以解释我吗?
You can use chai.http for this, which allows you to test HTTP apps. 您可以为此使用chai.http ,它可以测试HTTP应用程序。 Just make sure to export your app
as you already do. 只要确保已导出app
。
// api.spec.js
const chai = require('chai')
const chaiHttp = require('chai-http')
const server = require('../app.js')
chai.should()
chai.use(chaiHttp)
describe('User permissions', () => {
it('sets the user permissions', () => {
// Notice we are sending the request to the `server` export instead
// of a URL
return chai.request(server)
.post('/set-permission')
.then(res => {
res.should.have.status(200)
// add more tests here as you see fit
})
.catch(err => {
throw err
})
})
})
Also note that this more of an integration test rather than a unit test. 还要注意,这更多的是集成测试而不是单元测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.