I am trying to test my custom method on my Mongoose model, but the value I set in the test model disappears and makes the test fail. The test looks like this:
it('should get the friend id', function(){
var conversation = new Conversation({
'users': [new ObjectId('0'), new ObjectId('1')]
});
expect(conversation.getFriendId('0')).toEqual(new ObjectId('1'));
});
My model declaration contains this:
var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;
var ConversationSchema = new mongoose.Schema({
'users': [{'type': ObjectId, 'ref': 'User'}]
});
ConversationSchema.methods.getFriendId = function(userId) {
return this.users;
//return this.users[0] === new ObjectId(userId)
// ? this.users[1] : this.users[0];
};
module.exports = mongoose.model('Conversation', ConversationSchema);
When I run the test, I get:
Expected [ ] to equal { path : '1', instance : 'ObjectID', validators : [ ], setters : [ ], getters : [ ], options : undefined, _index : null }.
I set the users in the test, so the return value should be the array of users. (Which will still fail the test in it's current state, but should theoretically pass when uncommenting the second return statement.) Instead the users array shows up as empty.
How do I get the value from the test model to appear in my custom function?