Testing in nodejs project is new for me. I have testing for segment models, when run gulp test then it show error.
Here code,
//segment.model.js
'use strict';
import crypto from 'crypto';
var mongoose = require('bluebird').promisifyAll(require('mongoose'));
import { Schema } from 'mongoose';
var SegmentSchema = new Schema({
name: {type:String,required: true},
attribute: [{
name: String,
uiName: String,
type: { type: String },
value: String,
min: Number,
max:Number,
status:Boolean
}],
createdBy: {
type: Schema.Types.ObjectId,
ref: 'User',
required:true
},
});
SegmentSchema.methods={
checkAttributeLength(attribute){
return (attribute.length > 0)?true:false;
}
}
export default mongoose.model('Segment', SegmentSchema);
Testing File:
//segment.model.spec.js
'use strict';
import app from '../..';
import Segment from './segment.model';
import mongoose from 'mongoose';
// var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var segment;
var getSegment = function() {
segment = new Segment({
"name": "Segment 34312",
"createdBy": "56f8ff4a769fae4f7f88c456",
"attribute": [{
"name": "city",
"uiName": "City",
"type": "text",
"value": "NEWJERSY",
}, {
"name": "zip",
"uiName": "Zip code",
"type": "text",
"value": "123456",
}, {
"name": "number_of_children",
"uiName": "No. of children",
"type": "range",
"min": 12,
"max": 14,
}, {
"name": "pets_dog",
"uiName": "Dog owner",
"type": "binary",
"status": true,
}, {
"name": "financial",
"uiName": "MO: financial",
"type": "select",
"value": "M",
}],
});
return segment;
};
describe('Segment Model', function() {
before(function() {
// Clear users before testing
return Segment.removeAsync();
});
beforeEach(function() {
getSegment();
});
afterEach(function() {
return Segment.removeAsync();
});
it('should begin with no segments', function() {
return expect(Segment.findAsync({})).to
.eventually.have.length(0);
});
describe('#segment_name', function() {
it('should fail when saving with blank segment name', function() {
segment.name = '';
return expect(segment.saveAsync()).to.be.rejected;
});
});
describe('#segment_createdBy', function() {
it('should fail when saving without user id which created segment', function() {
segment.createdBy ='';
return expect(segment.saveAsync()).to.be.rejected;
});
});
describe('#segment_attribute', function() {
it('should fail when attribute length is zero', function() {
segment.attribute=[];
return expect(segment.checkAttributeLength(segment.attribute)).to.be.false;
});
});
});
Error:
..... ..... #segment_name
✓ should fail when saving with blank segment name Unhandled rejection ValidationError: Path
nameis required. at MongooseError.ValidationError (/home/chotelal/nrich/node_modules/mongoose/lib/error/validation.js:22:11) at model.Document.invalidate (/home/chotelal/nrich/node_modules/mongoose/lib/document.js:1338:32) at /home/chotelal/nrich/node_modules/mongoose/lib/document.js:1206:17 at validate (/home/chotelal/nrich/node_modules/mongoose/lib/schematype.js:693:7) at /home/chotelal/nrich/node_modules/mongoose/lib/schematype.js:724:9 at Array.forEach (native) at SchemaString.SchemaType.doValidate (/home/chotelal/nrich/node_modules/mongoose/lib/schematype.js:698:19) at /home/chotelal/nrich/node_modules/mongoose/lib/document.js:1204:9 at _combinedTickCallback (node.js:376:9) at process._tickDomainCallback (node.js:431:11) ..... ....
Aucun commentaire:
Enregistrer un commentaire