Skip to content

Instantly share code, notes, and snippets.

@nkzawa
Created March 12, 2013 23:40
Show Gist options
  • Save nkzawa/5148103 to your computer and use it in GitHub Desktop.
Save nkzawa/5148103 to your computer and use it in GitHub Desktop.
This test fails.
var assert = require('assert'),
mongoose = require('mongoose'),
Schema = mongoose.Schema,
connection;
beforeEach(function(done) {
connection = mongoose.createConnection('mongodb://localhost/test');
connection.on('open', function() {
connection.db.dropDatabase(done);
});
});
afterEach(function(done) {
connection.db.dropDatabase(function() {
connection.close(done);
});
});
describe('Document.prototype.toJSON', function() {
it('should respect schema options', function(done) {
var userSchema, User, groupSchema, Group;
userSchema = Schema({name: String});
// includes virtual path when 'toJSON'
userSchema.set('toJSON', {getters: true});
userSchema.virtual('hello').get(function() {
return 'Hello, ' + this.name;
});
User = connection.model('User', userSchema);
groupSchema = Schema({
name: String,
_users: [{type: Schema.ObjectId, ref: 'User'}]
});
// This test succeeds if you remove the following comment out.
// groupSchema.set('toJSON', {getters: true});
Group = connection.model('Group', groupSchema);
User.create({name: 'Alice'}, {name: 'Bob'}, function(err, alice, bob) {
if (err) done(err);
new Group({name: 'mongoose', _users: [alice, bob]}).save(function(err, group) {
Group.findById(group).populate('_users').exec(function(err, group) {
assert(group.toJSON()._users[0].hello);
done(err);
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment