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