Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose后中间件测试_Node.js_Unit Testing_Testing_Express_Mongoose - Fatal编程技术网

Node.js Mongoose后中间件测试

Node.js Mongoose后中间件测试,node.js,unit-testing,testing,express,mongoose,Node.js,Unit Testing,Testing,Express,Mongoose,我有两个猫鼬模型页面和缩略图 var Page = new Schema({ title: { type: String, default: '', trim: true }, url: { type: String, default: '', trim: true }, thumbnails: {type: Schema.ObjectId, ref: 'Thumbnails'}, }); Page.post('save', function(doc)

我有两个猫鼬模型页面和缩略图

  var Page = new Schema({
    title: { type: String, default: '', trim: true },
    url:   { type: String, default: '', trim: true },
    thumbnails: {type: Schema.ObjectId,  ref: 'Thumbnails'},
  });

  Page.post('save', function(doc) {
   if(doc.thumbnails) return;
   Thumbnail.generate(doc.url, function(err, thumb){
     doc.thumbnails = thumb;
     doc.save()
   });
  });

 var Thumbnail = new Schema({
    domain: {type: String, trim: true},
    url: {
      small: { type: String, trim: true },
      original: {type: String, trim: true }
    }
 });

 Thumbnail.statics.generate = function(doc, cb) {
   // generate site preview , save the thumbnail and call cb
 }
我正在使用mongoose post中间件生成缩略图。
如何在Post中间件中测试是否调用了缩略图.generate


谢谢

只需将中间件功能移动到一个模块,这样您就可以在测试中需要它,然后单独测试它。p、 我打赌它会抛出未定义的
无法读取属性生成错误。添加
var-Thumbnail=connection.model('Thumbnail')在使用它之前。hi@EugeneKostrikov,所以你的想法是公开post函数并覆盖该函数,对吗?你能写下你的想法,而不用担心语法或其他错误吗。我真正想了解的是想法。你怎么能改变这个要点?当然,在这里,中间件中唯一的逻辑是决定是否需要创建拇指。因此,您可以简单地模拟您测试的文档,并向中间件提供已知参数。谢谢,@EugeneKostrikov。使用javascript/nodejs提取和模拟是测试内部函数的最佳方法。我所做的是导出要在测试环境中存根的内部函数。谢谢