2017-03-06 9 views
2

몽구스 모델을 jest으로 모의하려고하는데, Cannot create property 'constructor' on number '1' 오류가 발생합니다. 아래에 표시된 두 개의 파일로 프로젝트를 생성하여 문제를 재현 할 수있었습니다. 몽구스 모델을 jest으로 조롱하는 방법이 있습니까?몽구스 몽구스 모델 조롱

이 ./model.js

const mongoose = require('mongoose') 
const Schema = mongoose.Schema 

const schema = new Schema({ 
    name: String 
}) 

module.exports = mongoose.model('Test', schema) 

./model.test.js

jest.mock('./model') 
const Test = require('./model') 

// Test.findOne.mockImplementation =() => { 
// ... 
// } 

오류 :

FAIL ./model.test.js 
    ● Test suite failed to run 

    TypeError: Cannot create property 'constructor' on number '1' 

     at ModuleMockerClass._generateMock (../../jitta/sandbox/rest_api/node_modules/jest-mock/build/index.js:458:34) 
     at Array.forEach (native) 
     at Array.forEach (native) 
     at Array.forEach (native) 

업데이트 :

는 것 같다 농담에 벌레. 내가이 문제를 해결하기 위해이 패키지를 작성할 수 있도록 https://github.com/facebook/jest/issues/3073

+0

농담에 대해서는 잘 모르겠지만 [mockgoose] (https://github.com/mockgoose/mockgoose)는 모든 것을 조롱하려고하는 것보다 쉬운 선택이었습니다. – Matt

답변

1

좋아, 내가 같은 문제가 있었다 :

import mongoose from 'mongoose'; 
const { Schema } = mongoose; 

const schema = Schema({ 
    name: String, 
    email: String, 
    created: { type: Date, default: Date.now } 
}) 

export default mongoose.model('User', schema); 

: https://www.npmjs.com/package/mockingoose

이 당신의 말을하자 그것을 사용하는 방법입니다이 모델입니다 이 테스트는 다음과 같습니다.

it('should find',() => { 
    mockingoose.User.toReturn({ name: 2 }); 

    return User 
    .find() 
    .where('name') 
    .in([1]) 
    .then(result => { 
    expect(result).toEqual({ name: 2 }); 
    }) 
}); 

더 많은 예제를 보려면 testsure 폴더를 체크 아웃하십시오. https://github.com/alonronin/mockingoose/blob/master/tests/index.test.js

데이터베이스에 연결되지 않았습니다.

+0

훌륭한 직업, 당신이 말하는 것과 똑같습니다! 고맙습니다! –

+0

중첩 된 문서는 어떻게됩니까? –