node.js - 在nodejs中使用(mocha,chai)进行错误单元测试
问题描述
运行测试时出现错误:mocha test.js
.
这是错误:
const chaiHttp = require('chai-http');
const sinon = require('sinon');
const sinontest = require('sinon-test');
const test = sinontest(sinon);
const chai = require('chai');
const accounts_controller = require('../controllers/accountsController');
const tickets_controller = require('../controllers/ticketsController');
const gains_controller = require('../controllers/gainsController');
const config = require('./config');
const router = config.serverdev;
const { expect } = chai;
const accounts_spec = require('./accountsSpec');
//configuration of chai
chai.use(chaiHttp);
chai.should();
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();
});
});
});
});
解决方案
您必须使用chai-http
插件并调用chai.should
:
let chaiHttp = require('chai-http');
let should = chai.should();
chai.use(chaiHttp);
其次,你的错误是Cannot read property 'should' of undefined
如此。请检查您创建成功的帖子(不带)res
undefined
err
推荐阅读
- spring - feign 客户端 - 从服务中检索 jwt 令牌并在 feign 客户端中自动设置它
- html - 我想做一个冻结的标题
- python - 如何将jupyter笔记本颜色主题放入vs代码
- azure - 如何调用 azure web 作业并传递参数
- sharepoint - SPFolder.MoveTo 保留子文件夹的元数据,同时重置根文件夹的元数据
- python - Cython 为应该计算为 0.5 的表达式返回 0?
- android - PN532 模拟卡无法被 Android 手机读取
- pywinauto - 运行代码 python3.7 和 pywinauto 时 pywinauto 出错
- c# - 如何在不访问其他设置的情况下打开 Windows 10 毫秒设置特定页面
- javascript - 带有下拉菜单的 ASP.NET 表单中的验证控件