Mongoose 모델에서 사용자 정의 메소드를 테스트하려고하지만 테스트 모델에서 설정 한 값이 사라지고 테스트가 실패하게됩니다. 시험은 다음과 같습니다Jasmine-Node로 테스트 할 때 Mongoose 모델 사용자 정의 함수에서 사라지는 값
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'));
});
내 모델 선언이 포함되어 있습니다
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);
내가 테스트를 실행하면, 내가 얻을 :
Expected [ ] to equal { path : '1', instance : 'ObjectID', validators : [ ], setters : [ ], getters : [ ], options : undefined, _index : null }.
내가 테스트에서 사용자를 설정, 따라서 반환 값은 사용자 배열이어야합니다. (테스트는 여전히 현재 상태에서 실패하지만 두 번째 return 문에서 주석을 제거 할 때 이론적으로 통과해야합니다.) 대신 사용자 배열이 비어있는 것으로 표시됩니다.
테스트 모델의 값을 내 사용자 지정 함수에 표시하려면 어떻게합니까?