2017-11-04 1 views
0

내가 노력하고 sinon.js라고하더라도, false를 돌려이 내가 시험에 노력하고 코드 조각입니다 :Sinon.js 스파이 생성자가 처음으로

class ErrorWithStatusCode extends Error{ 
    constructor(code, message, err){ 
     super(message); 
     this.code = code; 
     this.error = err; 
    } 
} 

export {ErrorWithStatusCode} 

는 이것은 err 오류 개체를 포함하더라도, 내 테스트에 실패, 내 테스트 파일

import {ErrorWithStatusCode} from '../../handlers/error.handler'; 
import chai from 'chai'; 
import sinon from 'sinon'; 
const should = chai.should(); 
const expect = chai.expect; 

describe('Error Class',()=>{ 
    it('the contructor function should be called once',()=>{ 
     const spyFunc = sinon.spy(ErrorWithStatusCode, 'constructor'); 
     const err = new ErrorWithStatusCode(500, 'Sorry, some error occurred.', {message: 'Some error'}); 
     console.log(err); 
     expect(spyFunc.calledOnce).to.be.true; 
    }) 
}); 

입니다하지만.

+0

이 테스트를 달성하기 위해 노력하고 정말로 확실하지? 그냥 조롱 한 기능을 호출했는지 확인하려는 것 같습니다. 생성자를 조롱 할 필요는 없습니다. 그것을 호출하고 반환 된 객체가 매개 변수로 전달 된 속성을 설정했는지 확인하십시오. – Alex

답변

0

에 스터 빙 클래스 constructor을 사용할 수 없습니다. Sinon 프로젝트의 메인테이너에서

: ES6 클래스에서

생성자 키워드는 문법 설탕입니다. 여전히 생성자 속성이 아니라 클래스를 만드는 함수 호출에 대해 실제로 테스트해야합니다.


링크 : https://github.com/sinonjs/sinon/issues/831#issuecomment-209648966