2017-11-04 8 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; 
    }) 
}); 

私のテストファイルです。しかし。クラスconstructorをスタブ

+0

このテストが達成しようとしているものは本当にわかりませんか?ちょうどあなたが嘲笑した機能を呼び出したことを確認しようとしているようですね。コンストラクタをモックする必要はありません。それを呼び出して、返されたオブジェクトにパラメータとして渡されたプロパティが設定されていることを確認します。 – Alex

答えて

0

Sinonでは不可能です。 Sinonプロジェクトのメンテナから

:ES6クラスで

コンストラクタキーワードは単なるシンタックスシュガーです。それでも、コンストラクタ・プロパティではなく、クラスを作成する関数の呼び出しを実際にテストする必要があります。


リンク:https://github.com/sinonjs/sinon/issues/831#issuecomment-209648966

関連する問題