草庐IT

javascript - 使用 Sinon 在同一文件中 stub 方法

我正在尝试对文件中的一个函数进行单元测试,同时对同一文件中的另一个函数进行stub,但没有应用模拟,而是调用了真实的方法。这是一个例子://file:'foo.js'exportfunctiona(){//.....}exportfunctionb(){letstuff=a();//calla//...dostuff}还有我的测试:import*asactionsfrom'foo';constaStub=sinon.stub(actions,'a').returns('mockedreturn');actions.b();//b()isexecuted,whichcallsa()ins

javascript - Sinon stub 是如何工作的?

在过去的几个月里,我一直在使用JavaScript并使用SinonJS来stub一些行为。我已经设法让它发挥作用,我使用了很多方法,一切都很好。但对于诗浓在幕后的运作方式,我还是有些疑问。我想我说的是Sinon,但这个问题可能适用于所有其他旨在模拟/stub/spy的库。过去几年我使用最多的语言是Java。在Java中,我使用Mockito来模拟/stub依赖项和依赖项注入(inject)。我曾经导入类,用@Mock注释字段并将此模拟作为参数传递给被测类。我很容易看出我在做什么:模拟一个类并将模拟作为参数传递。当我第一次开始使用SinonJS时,我看到了这样的东西:moduleUnde

javascript - sinon stub 不替换功能

我正在尝试使用sinonstub来替换可能需要时间的函数。但是当我运行测试时,测试代码似乎没有使用sinonstub。这是我要测试的代码。functiontakeTooLong(){returnreturnSomething();}functionreturnSomething(){returnnewPromise((resolve)=>{setTimeout(()=>{resolve('ok')},1500)})}module.exports={takeTooLong,returnSomething}这是测试代码。constchai=require('chai')chai.use(r

javascript - 我怎样才能用 sinonjs stub 内部引用的函数?

我有一个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

javascript - meteor JS : How to stub validated method in unit test

我正在使用经过验证的方法(mdg:validated-method)和LoggedInMixin(tunifight:loggedin-mixin)。现在我的单元测试出现了问题,因为它们因notLogged错误而失败,因为在单元测试中当然没有登录用户。我怎么必须stub呢?方法constresetEdit=newValidatedMethod({name:'reset',mixins:[LoggedInMixin],checkLoggedInError:{error:'notLogged'},//单元测试describe('resetEdit',()=>{it('shouldreset

javascript - 如何多次断言 stub 提取

使用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

javascript - sinon.stub() vs sinon.sandbox.stub()?

在我们的前端单元测试中使用sinon和sinon-qunit,我很难理解这些方法的区别。我们正在使用sinon.sandbox.stub()(字面意思是函数,我们不创建沙箱)并且这些stub显然会在每次测试后自动恢复。我只是在文档中的任何地方都看不到这一点。我不认为存在这种方法,我认为您需要使用sinon.sandbox.create()显式创建沙箱。在该沙箱对象上,您将调用stub函数,即mySandbox.stub(),而不是"sinon.sandbox.stub()"。谁能帮我理解一下? 最佳答案 stub-Sinon.JSs

javascript - 模拟/ stub 构造函数

我有以下代码:classClientsconstructor:->@clients=[]createClient:(name)->client=newClientname@clients.pushclient我正在用JasmineBDD像这样测试它:describe'TestConstructor',->it'shouldcreateaclientwiththenamefoo',->clients=newclientsclients.createClient'Foo'Client.should_have_been_called_with'Foo'it'shouldaddFootocli

javascript - 在 Jasmine 测试中 stub e.preventDefault()

我最近在我的一个javascript函数中添加了一个e.preventDefault(),它破坏了我的jasmine规范。我试过spyOn(e,'preventDefault').andReturn(true);但我得到eisundefined错误。如何stube.preventDefault()?showTopic:function(e){e.preventDefault();midParent.prototype.showTopic.call(this,this.model,popup);this.topic.render();}it("callstheparent",functi

javascript - 用 sinon stub 条纹 - 使用 stub.yields

我正在尝试stubnodejsstripeapi使用sinon,使用如下所示的测试来测试客户的创建:varsinon=require('sinon');varstripe=require('stripe');varcontroller=require('../my-controller');varstub=sinon.stub(stripe.customers,'create');stub.create.yields([null,{id:'xyz789'}]);//stub.create.yields(null,{id:'xyz789'});//sameresultwithorwith