Models not loading properly when trying to use Mocha testing
express, gruntjs, javascript, mocha.js, node.js
Solution
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');
Problem
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?