Javascript 为什么setTimeout中的expect语句不运行
我的代码:Javascript 为什么setTimeout中的expect语句不运行,javascript,mocha.js,settimeout,sinon,Javascript,Mocha.js,Settimeout,Sinon,我的代码: before(function _before(){ this.spyLog = sinon.spy(this.myOBJ.log, 'warn'); }); after(function _after(){ this.spyLog.restore(); }); it('should', sinon.test(function() { const stubFindOne = this.stub(this.myOBJ, 'findOne'
before(function _before(){
this.spyLog = sinon.spy(this.myOBJ.log, 'warn');
});
after(function _after(){
this.spyLog.restore();
});
it('should', sinon.test(function() {
const stubFindOne = this.stub(this.myOBJ, 'findOne');
stubFindOne.returns(bluebird.reject(new Error('Should have failed')));
this.myOBJ.toBeTestedFunction({});
setTimeout(function _expect() {
console.log('++++++++++++ 1 ++++++++++++++++++++++++++++++++++++++++');
console.log('this.spyLog', this.spyLog.args);
expect(this.spyLog.args[0][0].toString()).to.be.equal('Error: Should have failed');
console.log('++++++++++++ 2 ++++++++++++++++++++++++++++++++++++++++');
}, 100);
}));
为什么\u expect
功能不运行?谢谢
更新
尝试了以下代码,但出现错误:超时超过2000ms
。我想这是因为done
没有被调用。我需要这个.myOBJ.toBeTestedFunction({})代码>完成运行,然后运行期望值。如何修复代码
it('should', sinon.test(function(done) {
const stubFindOne = this.stub(this.myOBJ, 'findOne');
stubFindOne.returns(bluebird.reject(new Error('Should have failed')));
this.myOBJ.toBeTestedFunction({});
setTimeout(function _expect() {
console.log('++++++++++++ 1 ++++++++++++++++++++++++++++++++++++++++');
console.log('this.spyLog', this.spyLog.args);
expect(this.spyLog.args[0][0].toString()).to.be.equal('Error: Should have failed');
console.log('++++++++++++ 2 ++++++++++++++++++++++++++++++++++++++++');
done();
}, 100);
}));
更新
我的以下代码工作。但如何改进呢
it('should', sinon.test(function() {
const stubFindOne = this.stub(this.myOBJ, 'findOne');
stubFindOne.returns(bluebird.reject(new Error('Should have failed')));
return new bluebird.Promise((resolve, reject) => {
this.myOBJ.tobetestedFunction();
resolve();
})
.delay(1000)
.then(() => {
expect(this.spyLog.args[2][0].toString()).to.be.equal('Error: Should have failed');
});
}));
如前所述,测试用例在任何异步代码(如对setTimeout
的回调)可以运行之前就结束了。将done
参数添加到it
调用中,并在setTimeout
回调结束时调用done()
也就是说,我想我应该回去整理一下我所说的代码样本,这让我更仔细地看一下你的代码。。。这看起来会给你带来麻烦。看起来您正在预测一些异步事件,以使您的期望符合要求,但不是控制这些事件的执行,只是说“呃,它们应该在100个刻度内完成…”这不是一个好计划
就是说,它看起来像
it('should', sinon.test(function(done) {
const stubFindOne = this.stub(this.myOBJ, 'findOne');
stubFindOne.returns(bluebird.reject(new Error('Should have failed')));
this.myOBJ.toBeTestedFunction({});
setTimeout(function _expect() {
console.log('++++++++++++ 1 ++++++++++++++++++++++++++++++++++++++++');
console.log('this.spyLog', this.spyLog.args);
expect(this.spyLog.args[0][0].toString()).to.be.equal('Error: Should have failed');
console.log('++++++++++++ 2 ++++++++++++++++++++++++++++++++++++++++');
done();
}, 100);
}));
更新-您提到添加完成
无效(超时错误);这几乎可以肯定,因为围绕函数放置的sinon.test
包装器正在妨碍您。我不熟悉sinon
(而且谷歌也没有快速找到test
方法的文档),但一般来说,您需要的是sinone.test
返回的函数(1)获取一个done
参数,并(2)将其作为第一个参数传递给它包装的回调。在函数中放入done
参数显然并不能实现这一点。您必须查阅sinon
文档。(您尝试使用的jasmine功能是“异步测试”,但由于sinon试图不依赖于框架,我不确定他们是否/如何解决这个问题。可能只是一些需要附加参数的测试方法?)
关于为什么这不是一个好的测试方法的后续问题:对未在实时计算环境中编码的事件施加实时约束从来都不是一个好主意。如果需要250个滴答声才能满足条件,那真的是测试失败吗?如果你是在测试性能,而不是功能性,那么即使这样,你这里的测试可能会在某些硬件上通过,而在其他硬件上失败,或者测试“某些时候”。。。这并不是您想要从自动化测试套件中获得的行为
另外,您等待的那些异步事件几乎肯定是您的测试所依赖的行为的外部代码——在这种情况下,它不是一个合适的单元测试
以“正确的方式”进行此类测试显然更为困难,因为您会模拟外部依赖关系,并使用模拟的响应将您的测试推向预期评估能否请您提供更多有关的详细信息这不是一个好计划
?是的。我同意。但我的工作是为现有代码编写测试,我现在不应该更改这些代码。如何为异步函数编写适当的单元测试?