【发布时间】:2017-11-04 11:08:23
【问题描述】:
我是第一次尝试 sinon.js,这是我要测试的一段代码:
class ErrorWithStatusCode extends Error{
constructor(code, message, err){
super(message);
this.code = code;
this.error = err;
}
}
export {ErrorWithStatusCode}
这是我的测试文件
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;
})
});
但是,我的测试失败了,即使 err 包含错误对象。
【问题讨论】:
-
不太确定这个测试试图完成什么?看起来您正在尝试验证您调用了刚刚模拟的函数?您不需要模拟构造函数;调用它并检查返回的对象是否设置了您作为参数传入的属性。