JohanObrink/mongoose-mock — pull request #1 by sebmartel (2014-03-22 23:16:31+00:00)
Title: Models are stored and can be retrieved by name.

Here is a patch that allows for #model() to store Models by name. It is typical to use mongoose this way:

model.js
```javascript
var mongoose = require('mongoose');

var MyModel = new mongoose.Schema();
mongoose.model('MyModel', MyModel); // no exports
```

controller.js
```javascript
var mongoose = require('mongoose')
, MyModel = mongoose.model('MyModel');

exports.create = function(req, res, next) {
var newItem = new MyModel(...);
}
```

test.js
```javascript
var mmock = require('mongoose-mock')
, proxyquire = require('proxyquire')
, chai = require('chai')
, sinon = require('sinon')
, sinonChai = require('sinon-chai')
;

describe('controller test', function() {
var controller
, modelStub;

before( function() {
proxyquire('model.js', { 'mongoose': mmock });
modelStub = mongooseMock.model('MyModel'); // needs to return model created by
// the above proxyquire.
modelStub.... // add spies

// time to load the controller.
controller = proxyquire('controller.js', { 'mongoose': mmock }); // our stub model is loaded.
});

it( 'creates models', function() {
controller.create()
expect( modelStub )...
});
});

```

For another real use-case, this is how the [mean.io](https://github.com/linnovate/mean) sets up their framework with mongoose.

Code and test in pull request.
Cheers