Node.js .save()的存根不返回任何内容并超时
我需要用fixture断言naturalController.add()的返回 我承诺在我的控制器中添加函数,在我的模型中有一个在.save()之前调用的.pre()(在我看来,这似乎是问题的根源)。我不知道发生了什么,有人可以帮助我吗 测试的作用:Node.js .save()的存根不返回任何内容并超时,node.js,testing,mongoose,mocha.js,sinon,Node.js,Testing,Mongoose,Mocha.js,Sinon,我需要用fixture断言naturalController.add()的返回 我承诺在我的控制器中添加函数,在我的模型中有一个在.save()之前调用的.pre()(在我看来,这似乎是问题的根源)。我不知道发生了什么,有人可以帮助我吗 测试的作用: 实例化加载默认模型的控制器 调用.add()方法,并使用有效数据(它传递我省略的ajv) 这个类实例化了一个新文档(我认为问题就在这里) 该类调用文档实例的.save()方法(所有操作都在这里停止,我在mocha得到一个暂停时间) 在另一次测试中,
.add()
方法,并使用有效数据(它传递我省略的ajv).save()
方法(所有操作都在这里停止,我在mocha得到一个暂停时间)import sinon from 'sinon';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
import Bluebird from 'bluebird';
import NaturalPersonModel from './../../../src/models/naturalPersonModel';
import naturalPersonFixture from '../../fixtures/naturalPersonFixture';
describe('NaturalController', function() {
var stubSave,
naturalController = new NaturalController();
context('#add', function() {
beforeEach(function() {
stubSave = sinon.stub(NaturalPersonModel.prototype, "save");
});
afterEach(function() {
stubSave.restore();
});
it('resolve when data is ok', function() {
stubSave.returns(Bluebird.resolve(naturalPersonFixture.save));
return naturalController.add(naturalPersonFixture.add)
.then(function(value) {
chai.assert(value === naturalPersonFixture.save);
});
});
});
});
类别代码:
import Bluebird from 'bluebird';
import NaturalPerson from './../models/naturalPersonModel';
class NaturalController {
constructor() {
this.naturalPersonModel = NaturalPerson;
}
add(data) {
var newNP = new this.naturalPersonModel(data);
return newNP.save()
.catch(function(reason){
throw new Error(reason);
});
}
}
export default NaturalController;
型号:
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
import Bluebird from 'bluebird';
var autoIncrement = require('mongoose-auto-increment');
autoIncrement.initialize(mongoose.connection);
var naturalPersonSchemaBr = new mongoose.Schema({
naturalPersonData:{
personUUID: String,
nome: {type: String,required:true},
sobrenome: {type: String,required:true},
cpf: {type: String,required:true,unique:true},
rg: {type: String,required:true},
purchase:{
firstBuyStoreUUID: Number,
firstSellerUUID: Number
}
}
});
naturalPersonSchemaBr.methods.capitalizeFn = function(obj, list) {
String.prototype.capitalize = function() {
return this.split(' ').map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
};
list.forEach((attr) => {
if (obj[attr] && typeof(obj[attr]) === 'string') {
obj[attr] = obj[attr].toLowerCase().capitalize();
}
});
};
naturalPersonSchemaBr.plugin(autoIncrement.plugin, {
model: 'NaturalPerson',
field: 'UUIDcounter',
startAt: 1,
incrementBy: 1
});
naturalPersonSchemaBr.pre('save', function(next) {
var user = this;
user.naturalPersonData.personUUID = "NP"+ user.UUIDcounter;
console.log("user",user);
var list = ['nome','sobrenome','cpf','rg'];
naturalPersonSchemaBr.methods.capitalizeFn(user.naturalPersonData,list);
return next();
});
module.exports = mongoose.model('NaturalPerson', naturalPersonSchemaBr);
您一定是做错了什么,因为您的测试似乎正常工作:-)测试代码有一些错误,使它无法立即运行,例如fixture代码丢失,但我只是用常量替换了
import naturalPersonFixture..
,因为它只是上下文中的一个对象
我把所有的文件都上传了,作为我写作的要点。这个例子远不是很简单,但我还是通过这样做来实现:
mongod--dbpath/tmp/testdb
npm i mongoose mongoose mongoose按承诺自动增加摩卡柴sinon bluebird babel插件转换对象rest spread babel cli babel eslint babel插件转换类属性babel插件转换反应jsx babel-preset-es2015 babel根斜线导入
mocha--compilers js:babel register-s4 test.js
$ mocha --compilers js:babel-register test.js
NaturalController
#add
✓ resolve when data is ok (5ms)
1 passing (78ms)
模型的instance.save()超时。它在一个类中。由于它不返回任何东西(它被截短了),摩卡咖啡一直在等待,最后超时。我刚刚编辑了这个问题两件事
naturalController=newnaturalcontroller()
在每行之前-在.stub
行之后和stubSave.returns(Bluebird.resolve(naturalPersonFixture.save))代码>这是没有意义的,如果您没有任何要返回的内容,您应该在resolve中返回空对象Bluebird.resolve({})
我尝试了这两种方法,都没有效果。我认为Bluebird.resolve({})
是不正确的,因为我想断言一些东西以确保调用了newNP.save()。有一件事我没有提到:集成测试和对路由的邮递员调用确实有效。我就是不能做单元测试。继承似乎有问题,因为像.get(例如:newNP.get('name'))这样的文档方法确实适用于此单元测试代码。@Louis关于超时,这实际上是一个异步测试,因为他在测试中返回了一个承诺,mocha将其视为一个特例。@oligofren在我发表评论后对问题进行了编辑。原始代码如我在前面的评论中所述(肯定没有返回任何内容)。我已经删除了我原来的评论,但是这个问题仍然需要回答。好的。。将您的代码(包括packge)添加到我的代码中不起作用,但它在新项目中起作用。我会努力找到问题,谢谢你的帮助,我会在解决问题的时候发布我的发现。@ThiagoFreitas当类似的事情发生时,一个常见的罪魁祸首就是node_模块。尝试删除该文件夹并重新安装所有内容。我删除并重新安装,最终成功。我一点也不知道是什么问题。非常感谢,问题已经解决了,我只能接受,但我不允许投票,因为我需要15%的声誉,一旦我被允许,我会这么做。