Javascript 使用'should'测试nodeJS应用程序时出错(未定义不是函数)
我希望使用mocha测试和REST API,因此我有以下代码来测试创建操作:Javascript 使用'should'测试nodeJS应用程序时出错(未定义不是函数),javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,我希望使用mocha测试和REST API,因此我有以下代码来测试创建操作: 'use strict'; var should = require('should'); var assert = require('assert'); var request = require('supertest'); var winston = require('winston'); describe('Routing', function() { describe('asset', fun
'use strict';
var should = require('should');
var assert = require('assert');
var request = require('supertest');
var winston = require('winston');
describe('Routing', function() {
describe('asset', function() {
it('Crear un nuevo activo en la base de datos y retornarlo', function(done) {
var asset = {
"description" : "Computador portatil",
"maker" : "Lenovo",
"module" : "Y410",
"serialNumber" : "123123",
"bardCode" : "21212",
"account" : 1212,
"usefulLife" : 24,
"downtimeCosts" : 200,
"purchasePrice" : 120000,
"assetState_id" : 1,
"assetCategory_id" : 3,
"assetSystem_id" : 3,
"supplier" : 1
};
request(url)
.post('/asset/create')
.send(asset)
.end(function(err, res) {
if (err) {
throw err;
}
console.log (res.should.have);
res.should.have.status(200);
done();
});
});
});
});
POST操作如下所示:
router.post('/create', function(req, res, next) {
models.asset.create(req.body
).then(function(asset) {
res.send(asset.dataValues)
}).catch(function(error) {
console.log (error);
res.status(500).send(error);
});
});
运行测试时,得到以下错误uncaughttypeerror:undefined不是res.should.have.status(200)行中的函数
我想原因是因为我没有明确说明状态响应,所以我将res更改为`res.status(200).send(asset.dataValues),但不起作用请参见
我认为您应该尝试以下代码:
should(res).have.property('status', 200);
它解决了res没有属性“.should”的问题。谢谢你的回答,我使用
sudo npm install chai安装chain——保存dev
并更改vars,但是错误仍然存在。你检查了我发布的URL了吗?你在用摩卡跑鞋吗?
should(res).have.property('status', 200);