我正在尝试使用sinonstub来替换可能需要时间的函数。但是当我运行测试时,测试代码似乎没有使用sinonstub。这是我要测试的代码。functiontakeTooLong(){returnreturnSomething();}functionreturnSomething(){returnnewPromise((resolve)=>{setTimeout(()=>{resolve('ok')},1500)})}module.exports={takeTooLong,returnSomething}这是测试代码。constchai=require('chai')chai.use(r
如何使用sinon模拟调用回调的外部方法?给定以下代码的示例,getText应在回调函数中返回'astring'作为响应sinon.stub(a,'getText').returns('astring')letcb=function(err,response){console.log(response)}a.getText('abc',cb)它应该产生输出'astring'因为它调用回调函数cb但是没有输出 最佳答案 sinon.stub(a,'getText').yields(null,'astring');yields()将使用
我想编写一个测试,检查我的函数是否使用await关键字调用其他函数。我希望我的测试失败:asyncmethodA(){this.methodB();returntrue;},我希望我的测试能够成功:asyncmethodA(){awaitthis.methodB();returntrue;},我也想让成功地使成为我的测试:methodA(){returnthis.methodB().then(()=>true);},我有一个解决方案,方法是使用process.nextTick对该方法进行stub并强制其在其中返回假promise,但这似乎很丑陋,我不想在测试中使用process.nex
我有一个myModuleNode.js模块,其中包含:functionb(){console.log('originalb');}functiona(){b();}exports.a=aexports.b=b;以及以下使用mocha+sinon.js的测试套件:constmyModule=require('./myModule.js');constsinon=require('sinon');constsinonChai=require('sinon-chai');chai.use(sinonChai);describe('notworkingstub',()=>{it('should
我有以下通过promise递归运行的队列消费者类:"usestrict";varqueue=require("./queue"),helpers=require("./helpers"),vendors=require("../config/vendors"),queueConf=require("../config/queue");functionConsumer(){this.queue=newqueue.TaskQueue();this.currentItem=null;this.port=null;this.payload=null;}Consumer.prototype.pa
我有一堆针对各种Angular(1.4.7)指令的工作单元测试,我正在使用Karma、Jasmine和Sinon进行测试。我正在尝试为新指令添加单元测试,这是我目前唯一使用$window的指令,但我在控制台输出中看到一个神秘错误:TypeError:'undefined'isnotanobject(evaluating'this.proxy.toString')此错误来自第2372行的sinon.js。我在指令单元测试中做所有“正常”的事情,例如创建一个将指令作为属性的假元素:testElement=document.createElement('div');testElement.s
是否有简单的方法来模拟hapi回复对象/函数以便于进行单元测试?我看到的hapi示例都使用server.inject和“实验室”框架进行测试。我很想知道如何继续使用mocha,并希望直接测试Controller而不是注入(inject)服务器。我应该使用sinon来模拟回复对象吗?测试/post.jsbefore(function(){PostController=proxyquire('../controllers/post',{'mongoose':mongooseMock});});it('shouldbeabletocreateapost',function(done){var
使用proxyquire、sinon和mocha。我能够在第一次调用fetch时stubfetch。但是在递归的第二个获取调用中,我无法断言它。从输出来看,断言可能会在测试完成之前运行。您将在断言后通过secondfetch控制台看到这一点。索引.jsvarfetch=require('node-fetch');functiona(){console.log('functionaruns');fetch('https://www.google.com').then((e)=>{console.log('firstfetch');b();}).catch((e)=>{console.lo
在我们的前端单元测试中使用sinon和sinon-qunit,我很难理解这些方法的区别。我们正在使用sinon.sandbox.stub()(字面意思是函数,我们不创建沙箱)并且这些stub显然会在每次测试后自动恢复。我只是在文档中的任何地方都看不到这一点。我不认为存在这种方法,我认为您需要使用sinon.sandbox.create()显式创建沙箱。在该沙箱对象上,您将调用stub函数,即mySandbox.stub(),而不是"sinon.sandbox.stub()"。谁能帮我理解一下? 最佳答案 stub-Sinon.JSs
我有这个功能:letremovePresentation=function(presentationName,callback){letrimraf=require('rimraf');callback();callback();callback();if(!presentationName||!presentationName.trim()){callback();return;}presentationName=presentationName.replace('.zip','');rimraf('./presentations/'+presentationName,functio