我想使用 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', 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);
});
});
运行测试时出现以下错误 Uncaught TypeError: undefined is not a function
在行 res.should.have.status(200);
我想原因是因为我没有明确说明状态响应,所以我将 res 更改为 `res.status(200).send(asset.dataValues) 但不起作用
最佳答案
我认为你应该尝试下面的代码:
should(res).have.property('status', 200);
它解决了 res 没有属性“.should”的问题。
关于javascript - 使用 `should` 测试 nodeJS 应用程序时出错(undefined 不是函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33152392/