尝试使用摩卡testing时,模型加载不正确
我正在尝试使用摩卡来testing我的快速应用程序。 我的文件夹结构是:
myapp |-app |--models |-test |--mocha-blanket.js |--mocha |--karma |-server.js
server.js
是我的快递服务器。 我以前在options.require
,但文件说,使用blanket.js
。 我的mocha-blanket.js
是:
var path = require('path'); var srcDir = path.join(__dirname, '..', 'app'); require('blanket')({ // Only files that match the pattern will be instrumented pattern: srcDir });
我的Gruntfile有:
mochaTest: options: reporter: "spec" require: 'test/mocha-blanket.js' # require: "server.js" coverage: options: reporter: 'html-cov', captureFile: 'mocha-coverage.html' src: ["test/mocha/**/*.js"]
我得到的错误是:
>> Mocha exploded! >> MissingSchemaError: Schema hasn't been registered for model "Company". >> Use mongoose.model(name, schema) >> at Mongoose.model (/myapp/node_modules/mongoose/lib/index.js:315:13) >> at Object.<anonymous> (/myapp/test/mocha/controllers/company.js:4:22) >> at Module._compile (module.js:456:26) >> at Module._extensions..js (module.js:474:10)
我确信我正在做一些事情(或很多事情)不正确。 但是我不确定是什么。 有任何想法吗?
我认为你的testing代码没有正确初始化你的应用程序,特别是Mongoose模式和模型的初始化。
在mocha/controllers/company.js
,你可能使用类似这样的代码:
var Company = mongoose.model('Company');
但是,如果跳过Company
模型的初始化(用模式连接模型),则会导致错误。
为了给出一个非常短的独立的例子,这段代码将会失败,同样的错误:
var mongoose = require('mongoose'); var Company = mongoose.model('Company');
这个代码,添加了初始化,工作正常:
var mongoose = require('mongoose'); mongoose.model('Company', new mongoose.Schema()); var Company = mongoose.model('Company');
你得到这个错误,因为你没有明确地“加载”你的模型mongoose。 我已经解决了这个问题:1模型初始化脚本:
module.exports = { storage:{ sample: require('models/storage/sample'), other models scoped to "storage" connection... }, accounts: { company: require('models/accounts/company'), etc... } };
这是在每个mongoose.createConnectioncallback中启动一次:
var accounts = mongoose.createConnection(config.get('mongoose:accounts:uri'), config.get('mongoose:accounts:options'), function(err){ if (err) throw err; console.log('connection to accounts database open'); require('models/init').accounts; });
它运行所有的模型声明代码,后来在testing中,你已经注册了所有的模型。
确保数据库连接中间件。 这个人让你确定连接是开放的,init脚本运行成功。
var storage = require('models').storage; var accounts = require('models').accounts; module.exports = function ensureDbConnectionMiddleware(req, res, next){ var count = 0; var interval = setInterval(function(){ if (storage._readyState === 1 && accounts._readyState === 1){ clearInterval(interval); return process.nextTick(function(){ next(); }); }else{ if (count++ > 50){ res.json({status: 0, message: 'Database offline'}); throw new Error('Database is offline'); } } }, 100); };
后来在testing中
var should = require('should'); var express = require('express'); var app = express(); app.use(express.bodyParser()); app.use(require('middleware/ensureDbConnection')); require('routes')(app); //etc...
我不确定这是否是最好的解决scheme,但效果很好。 当然有一个重要的警告:你必须保持所有这些东西同步。 但是,您可以尝试将其委派给咕噜声。
对于Mocha,在尝试使用模型之前,需要等待MongoDB连接打开。
var mongoose = require('mongoose'); var Company; describe('Unit Test', function () { before(function(done) { mongoose.connection.once('open', function() { Company = mongoose.model('Company'); done(); }); }); ... tests here ... });