in the name of GOD i wrote bellow code for product model for a shopping web app :
var mongoose = require('mongoose');
var Category = require('./category')
var q = require('q');
productSchema ={
'name' : {type : String ,
required : true},
'price' :{type : Number ,
required: tru`enter code here`e},
'pictures' : [{type : String , default :'http://ift.tt/2brmrJX'}],
'Category' : Category.categorySchema
};
var Product = new mongoose.Schema(productSchema);
var productModel = mongoose.model('productModel' ,Product ,'product');
//method
exports.insert = function (doc) {
var document = new productModel(doc);
var final = q.defer();
document.save(function (error) {
if(error)
final.reject(false);
else
final.resolve(true);
});
return final.promise;
};
and i wrote a test for this program :
var mongoose = require('mongoose');
var product = require('./product');
var assert = require('assert');
var config = require('./config');
var q = require('q');
mongoose.connect(config.testUri);
describe('product Test:' , function () {
var testInput = {'name' : 'calculase one' , 'price': 50 , 'pictures':['http://ift.tt/2bgnQGL'] , Category:{'_id' : 'One' , "parent" : "Calculase" , "ansestors" : ["One" , "Calculase" , "Books"]}};
var result = product.insert(testInput);
it('inserting into collection. ' , function () {
result.then(function (resultDoc) {
assert.equal(resultDoc.name ,true);
})
});
my test is passing.even I connect to database or not connect.I discover that my program does not enter in "then" scope and assert.equal method does not run.please guide me to solve this Problem. thank!
Aucun commentaire:
Enregistrer un commentaire