Reputation: 798
I want test and REST API using mocha, so i have the following code to test the create operation:
'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();
});
});
});
});
the POST operation is the following:
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);
});
});
When run the test get the following error Uncaught TypeError: undefined is not a function
in the line res.should.have.status(200);
I suppose that the reason was because i dont say explicit the status response so I change the res to `res.status(200).send(asset.dataValues) but don't work
Upvotes: 1
Views: 525
Reputation: 786
I think you should try the following code:
should(res).have.property('status', 200);
It solves the fact that res doesn't have the property ".should".
Upvotes: 1
Reputation: 1777
see http://chaijs.com/guide/styles/
var assert = require('chai').assert;
var should = require('chai').should();
Upvotes: 1