我想为我们的项目实现代码覆盖,所以我们使用 grunt-istanbul。我坚持这个教程:https ://github.com/taichi/grunt-istanbul
我的问题是,在针对工具化源代码运行测试时出现此错误,但在针对正常代码运行测试时它工作正常。
>> Mocha exploded!
>> MongooseError: Schema hasn't been registered for model "Comm".
>> Use mongoose.model(name, schema)
>> at Mongoose.model (/Users/user/repos/review.ninja/node_modules/mongoose/lib/index.js:322:13)
>> at Object.<anonymous> (/Users/user/repos/review.ninja/test/coverage/instrument/src/server/api/comm.js:9:147)
>> at Module._compile (module.js:449:26)
>> at Object.Module._extensions..js (module.js:467:10)
>> at Module.load (module.js:349:32)
>> at Function.Module._load (module.js:305:12)
>> at Module.require (module.js:357:17)
>> at require (module.js:373:17)
>> at /Users/user/repos/review.ninja/node_modules/grunt-mocha-test/node_modules/mocha/lib/mocha.js:172:27
>> at Array.forEach (native)
>> at Mocha.loadFiles (/Users/user/repos/review.ninja/node_modules/grunt-mocha-test/node_modules/mocha/lib/mocha.js:169:14)
我们也在mongoose.model('Comm', CommSchema);
comm 模块中导出并在测试代码中导入。如前所述,如果正常运行 Mocha 测试,它就可以工作。