试图使用摩卡测试时,模型加载不正确 [英] Models not loading properly when trying to use Mocha testing
问题描述
我正尝试使用摩卡来测试我的快递应用程序。我的文件夹结构是:
myapp
| -app
| --models
| - 测试
| --mocha-blanket.js
| --mocha
| --karma
| -server.js
server.js
是我的快递服务器。我之前在 options.require
中有过,但文档说要使用 blanket.js
。我的 mocha-blanket.js
是:
var path = require '路径');
var srcDir = path.join(__ dirname,'..','app');
require('blanket')({
//只有匹配模式的文件才会被检测到
pattern:srcDir
}); b
$ b > mochaTest:
选项:
记者:spec
需要:'test / mocha-blanket.js'
#require:server.js
覆盖率:
选项:
记者:'html-cov',
captureFile:'mocha-coverage.html'
src:[test / mocha / ** / *。js]
我得到的错误是:
>>摩卡爆炸了!
>> MissingSchemaError:架构尚未注册模型Company。
>>使用mongoose.model(名称,模式)
>>在Mongoose.model(/myapp/node_modules/mongoose/lib/index.js:315:13)
>>在Object。< anonymous> (/myapp/test/mocha/controllers/company.js:4:22)
>>在Module._compile(module.js:456:26)
>>在Module._extensions..js(module.js:474:10)
我相信我不正确地做某件事(或许多事)。但我不确定是什么。任何想法?我认为你的测试代码没有正确地初始化你的应用程序,特别是Mongoose模式和模型的初始化。
在 mocha / controllers / company.js
中,您可能使用类似下面的代码:
var Company = mongoose.model('Company');
但是,如果初始化 Company
model(在模型中用模式连接模型)被跳过。为了给出一个非常短的独立示例,这段代码会失败,并出现相同的错误:
var mongoose = require('mongoose');
var Company = mongoose.model('Company');
这段代码在添加了初始化的情况下可以正常工作:
var mongoose = require('mongoose');
mongoose.model('Company',new mongoose.Schema());
var Company = mongoose.model('Company');
I'm trying to use mocha to test my express app. My folder structure is:
myapp
|-app
|--models
|-test
|--mocha-blanket.js
|--mocha
|--karma
|-server.js
server.js
is my express server. I had that before in options.require
, but the documentation said to use a blanket.js
. My mocha-blanket.js
is:
var path = require('path');
var srcDir = path.join(__dirname, '..', 'app');
require('blanket')({
// Only files that match the pattern will be instrumented
pattern: srcDir
});
My Gruntfile has:
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"]
The error I'm getting is:
>> 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)
I'm sure I'm doing something (or a lot of things) incorrectly. But I'm not sure what. Any ideas?
I think your testing code isn't properly initializing your application, specifically initialization of the Mongoose schema and models.
In mocha/controllers/company.js
, you're probably using code similar to this:
var Company = mongoose.model('Company');
However, that will raise the error that you're getting if the initialization of the Company
model (where you hook up the model with the schema) was skipped.
To give a very short standalone example, this code will fail with the same error:
var mongoose = require('mongoose');
var Company = mongoose.model('Company');
This code, with the added initialization, works fine:
var mongoose = require('mongoose');
mongoose.model('Company', new mongoose.Schema());
var Company = mongoose.model('Company');
这篇关于试图使用摩卡测试时,模型加载不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!