2017-10-04 30 views
0

ES6 클래스의 메소드를 조롱하고 싶습니다.node.js 및 모카 테스트로 정의되지 않은 시논 스텁

나는 수입하고 모델 모듈 : 모델에서

// test.js 
const models = require(path.resolve('./models')); 

이 폴더는하는 index.js하고 models.user 호출하는 동안은 사용자 폴더에서하는 index.js로 리디렉션 : 다음

// models/index.js 
models.user = user; 

내가하는 index.js에서 사용자 클래스가 : // 모델/사용자 /하는 index.js

class User extends Model { 
    // simplified exists - it returns boolean or thows an error 
    static async exists(username) { 
    if (username) { 
     returns true 
    } else { 
     throw new Error('bad output'); 
    } 
    } 
} 

I 완 sinon 스텁이있는 스텁에 (사용자 이름) 메소드가 없습니다.

const sinon = require('sinon'); 
const models = require(path.resolve('./models')); 

describe.only('generateTokenBehavior()', function() { 
    it('should return 200 given valid username and password', function() { 
     ... 
     const stub = sinon.stub(); 
     stub(models.user.prototype, 'exists').callsFake(true); 
     ... 
    }); 

을 나는 스텁 라인에 오류가 점점 오전 :

내가 뭐하는 거지

TypeError: Cannot read property 'callsFake' of undefined 

이 코드에 어떤 문제가 있습니까? 비슷한 스택 질문에 대해이 문제를 연구했지만 대답을 찾지 못했습니다.

답변

1

여기서 문제 sinon.stub의 결과를 호출하면() 함수로 정의 를 반환한다.

const sinon = require('sinon'); 
const models = require(path.resolve('./models')); 

describe.only('generateTokenBehavior()', function() { 
    it('should return 200 given valid username and password', function() { 
     ... 
     const stub = sinon.stub(models.user.prototype, 'exists').callsFake(true); 
     ... 
    }); 
참고로

이 문서는 여기에 있습니다 : http://sinonjs.org/releases/v4.1.1/stubs/#properties

내가 당신이했던 방법을 쓰기 위해 당신을 비난하지 않습니다 - 문서가 조금 잘못된 것입니다.