.save() 的存根不返回任何内容并且超时

问题描述 投票:0回答:1

我需要用固定装置断言naturalController.add()的返回。

我在 Promise 的控制器中添加了函数 add ,并且在我的模型中有一个 .pre() (在我看来,这似乎是问题的根源),它在 .save() 之前调用,我不知道发生了什么,有人可以帮助我吗?

测试的作用:

  1. 实例化一个加载默认模型的控制器。
  2. 使用包含有效数据的 Promise 调用
    .add()
    方法(它传递了我省略的 ajv)。
  3. 该类实例化一个新文档(我认为问题就在这里)。
  4. 该类调用文档实例的
    .save()
    方法(一切都停止在这里,我在摩卡上暂停了)。

在另一次测试中一切正常。我可以通过邮递员和集成测试添加任意数量的文档。

测试:

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);
node.js testing mongoose mocha.js sinon
1个回答
-1
投票

你一定做错了什么,因为你的测试似乎有效:-)测试代码有一些错误,使其无法立即运行,例如夹具代码丢失,但我只是用常量替换了

import naturalPersonFixture ..
,看到它只是上下文中的一个对象。

我上传了所有文件作为要点,以便轻松下载和使用。这个例子远非最小,但我仍然通过这样做让它工作:

  1. 安装蒙戈
  2. 奔跑
    mongod --dbpath /tmp/testdb
  3. 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
  4. 设置 babel(参见要点链接)
  5. 运行测试:
    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)
© www.soinside.com 2019 - 2024. All rights reserved.