I'm trying to get mocha's --watch
option to work. It works fine, until I have to do anything with a mongoose model. Apparently Mongoose keeps some sort of cache, from my understanding, and the error I get has been tracked and closed. The problem is, I'm a bit new to this whole thing and need a little guidance how and where to put the things I need to get this to work. So, what I've tried:
mongoose.model
. Works, but obviously defeats the purpose of --watch
.mongoose.disconnect
) in the "after" block of my Mongoose suite.--watch
and running tests fresh every time.Of those three, obviously on the third works, and I'd really like to use all the features of my build tools. So, here's what I have. Where am I going wrong?
var mongoose = require('mongoose'),
register = require('./_register');
var userSchema = mongoose.Schema({
email: String,
password: String
});
userSchema.methods.setPassword = function(password) {
this.password = password;
};
module.exports = mongoose.model('User', userSchema);
var User = require('../models/user');
describe('User', function() {
describe('#setPassword()', function() {
it('should set the password', function() {
var user = new User();
user.setPassword('test');
user.password.should.not.equal('');
});
it('should not be in plaintext');
});
describe('#verifyPassword()', function() {
it('should return true for a valid password');
it('should return false for an invalid password');
});
});
Automate caching in 3 minutes. 1. Mongoose introduces a dedicated ODM (Object Data Modeling) library for working with MongoDB, which allows you to map objects and collection documents from a database.
Mongoose save with an existing document will not override the same object reference. Bookmark this question.
Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.
There's nothing built into Mongoose regarding migrating existing documents to comply with a schema change. You need to do that in your own code, as needed.
I had some success with running this in my afterEach() block:
delete mongoose.models.YourModel;
delete mongoose.modelSchemas.YourModel;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With