简体   繁体   English

如何对路由器功能NodeJS / Mocha / Chai进行单元测试

[英]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.

相关问题 如何使用 mocha/chai 测试 ejs/nodejs function? - How to test ejs/nodejs function with mocha/chai? 如何使用酶,摩卡,柴对单元自定义的React函数进行单元测试 - How do you unit test a custom React function using enzyme, mocha, chai 如何使用摩卡茶和酶对道具的状态进行单元测试? - How to unit test the state of props using mocha chai and enzyme? 如何为chai Expect提供用于摩卡单元测试的自定义错误消息? - How to provide chai expect with custom error message for mocha unit test? 如何使用vanillajs和nodejs在Mocha / Chai / Sinon中测试addEventListener? - How to test addEventListener in Mocha/Chai/Sinon with vanillajs and nodejs? Mocha - Chai Unit Terst 报告生成 - NodeJS - Mocha - Chai Unit Terst report generation - NodeJS 带有Mocha,Chai的函数时如何测试Javascript对象属性 - How to test a Javascript object property when function with mocha, chai 如何使用 mocha & chai 测试 js factory function - How to test js factory function using mocha & chai 如何使用mocha / chai / chai-as-promised测试ES7异步函数 - How to test an ES7 async function using mocha/chai/chai-as-promised Mocha / Chai测试linkTo函数返回AssertionError - Mocha/Chai Test linkTo Function Returning AssertionError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM