我需要使用fixture来断言naturalController.add()的返回。
我的函数在我的控制器中添加了promise,在我的模型中有一个.pre()(在我看来是问题的根源)在.save()之前被调用我不知道是什么碰巧,有人可以帮助我吗?
测试的作用:
.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);
答案 0 :(得分:-1)
你必须做错事,因为你的测试似乎有效:-)测试代码有一些错误,使得它不能立即运行,例如缺少夹具代码,但我只是替换了import naturalPersonFixture ..
看到它只是来自上下文的对象。
我上传了所有文件作为easy download and playing with的要点。这个例子远非微不足道,但我仍然通过这样做来实现它:
mongod --dbpath /tmp/testdb
npm i mongoose mongoose-auto-increment mocha chai-as-promised sinon bluebird babel-plugin-transform-object-rest-spread babel-cli babel-eslint babel-plugin-transform-class-properties babel-plugin-transform-react-jsx babel-preset-es2015 babel-root-slash-import
mocha --compilers js:babel-register -s 4 test.js
不完全是最小的!但它有效:D
$ mocha --compilers js:babel-register test.js
NaturalController
#add
✓ resolve when data is ok (5ms)
1 passing (78ms)