摩卡-手表和猫鼬模型

nic*_*nli 5 testing mocha.js mongoose mongodb node.js

如果我离开mocha监视更改,则每次保存文件猫鼬时都会引发以下错误:

OverwriteModelError:Client编译后无法覆盖模型

我知道猫鼬不允许两次定义模型,但是我不知道该如何使用它mocha --watch

// client.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var clientSchema = new Schema({
    secret: { type: String, required: true, unique: true },
    name: String,
    description: String,
    grant_types: [String],
    created_at: { type: Date, default: Date.now }
});

module.exports = mongoose.model('Client', clientSchema);
Run Code Online (Sandbox Code Playgroud)

这是测试

// client-test.js
var chai = require('chai');
var chaiHttp = require('chai-http');
var mongoose = require('mongoose');

var server = require('../../app');
var Client = require('../../auth/models').Client;

var should = chai.should();

chai.use(chaiHttp);

describe('client endpoints', function() {
    after(function(done) {
        mongoose.connection.close();
        done();
    });

    it('should get a single client on /auth/client/{clientId} GET', function(done) {
        var clt = new Client({
            name: 'my app name',
            description: 'super usefull and nice app',
            grant_types: ['password', 'refresh_token']
        });

        clt.save(function(err) {
            chai.request(server)
                .get('/auth/client/' + clt._id.toString())
                .end(function(err, res) {
                    res.should.have.status(200);
                    res.should.be.json;
                    res.body.should.have.property('client_id');
                    res.body.should.not.have.property('secret');
                    res.body.should.have.property('name');
                    res.body.should.have.property('description');
                    done();
                });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

Geo*_*rge 2

我遇到过同样的问题。我的解决方案是检查模型是否已创建/编译,如果没有,则执行此操作,否则只需检索模型。

使用mongoose.modelNames()您可以获得模型名称的数组。然后使用 .indexOf 检查您想要获取的模型是否在数组中。如果不是,则编译模型,例如:mongoose.model("User", UserSchema),但如果它已经定义(如 mocha --watch 的情况),只需检索模型(不要再次编译它),您可以使用例如:mongoose.connection.model("User")

这是一个返回一个函数来执行此检查逻辑的函数,该函数本身返回模型(通过编译它或只是检索它)。

const mongoose = require("mongoose");
//returns a function which returns either a compiled model, or a precompiled model
//s is a String for the model name e.g. "User", and model is the mongoose Schema
function getModel(s, model) {
  return function() {
    return mongoose.modelNames().indexOf(s) === -1
      ? mongoose.model(s, model)
      : mongoose.connection.model(s);
  };
}
module.exports = getModel;
Run Code Online (Sandbox Code Playgroud)

这意味着您对模型的要求必须有所不同,因为您可能会替换如下所示的内容:

module.exports = mongoose.model("User", UserSchema);
Run Code Online (Sandbox Code Playgroud)

它返回模型本身,如下所示:

module.exports = getModel("User", UserSchema);
Run Code Online (Sandbox Code Playgroud)

它返回一个函数来返回模型,通过编译它或只是检索它。这意味着当您需要“User”模型时,您需要调用 getModel 返回的函数:

const UserModel = require("./models/UserModel")();
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助。