在nodejs中使用(mocha,chai)进行错误单元测试

问题描述 投票:1回答:1

我在运行测试时遇到错误:mocha test.js这是错误:

Account
   create an admin account
     Should create a new admin account:
 Uncaught TypeError: Cannot read property 'should' of undefined
  at /api/test/accountsSpec.js:41:17
  at Test.Request.callback (node_modules/superagent/lib/node/index.js:728:3)
  at ClientRequest.<anonymous> (node_modules/superagent/lib/node/index.js:647:10)
  at TLSSocket.socketErrorListener (_http_client.js:432:9)
  at errorOrDestroy (internal/streams/destroy.js:128:12)
  at onwriteError (_stream_writable.js:463:3)
  at onwrite (_stream_writable.js:484:7)
  at internal/streams/destroy.js:60:7
  at TLSSocket.Socket._destroy (net.js:673:5)
  at TLSSocket.destroy (internal/streams/destroy.js:55:8)
  at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:93:12)

这是我的代码:

describe('Account', () => {
/* 
* create admin account group description
*/
describe('create an admin account', () => {
    /* 
    * test case to check if addadmintestaccount function exist 
    */
    it('Should exist',test(function(){
        expect(accounts_controller.addadmintestaccount).to.not.be.undefined;
    }));
    /* 
    * test case to create a new admin account
    */
    it('Should create a new admin account',(done) => {
        chai.request(router)
        .post('/api/test/addadmintestaccount')
        .set("Content-Type", "application/json")
        .end((err,res)=>{
        res.should.have.status(200);
        expect(res.body.success).to.equals(true);
        adminToken = res.body.result;
        exports.adminToken = adminToken;
        done();
        });
    });
});

});

我到处搜索,没有结果。拜托,有人可以帮我!谢谢

node.js mocha chai should.js
1个回答
0
投票

您必须使用chai-http插件并致电chai.should

let chaiHttp = require('chai-http');

let should = chai.should();

chai.use(chaiHttp);
© www.soinside.com 2019 - 2024. All rights reserved.