草庐IT

Test_stub

全部标签

javascript - Proxyquire 没有 stub 我需要的类(class)

我有一个类AProvider需要'./b.provider'。constBProvider=require('./b.provider');classAProvider{staticgetdefaultPath(){return`defaults/a/${BProvider.getThing()}`;}}module.exports=AProvider;b.provider.js与a.provider.js相邻,看起来像global.stuff.whatever=require('../models').get('Whatever');//Ididn'twritethis!classB

javascript - 为什么 RegEx.test 会在后续调用中更改结果?

这个问题在这里已经有了答案:WhydoesaRegExpwithglobalflaggivewrongresults?(7个答案)关闭7年前。为什么以下从true变为false;varr=/e/gi;r.test('e');//truer.test('e');//false然后继续切换true,false,true,false......

javascript - Regex.test() 给出真假顺序?

任何人都可以解释一下,为什么本地Regex变量和非本地Regex变量有不同的输出。varregex1=/a|b/g;functionisAB1(){returnregex1.test('a');}console.log(isAB1());//trueconsole.log(isAB1());//falseconsole.log(isAB1());//trueconsole.log(isAB1());//falsefunctionisAB2(){varregex2=/a|b/g;returnregex2.test('a');}console.log(isAB2());//truecons

javascript - 我如何使用 Sinon stub $(window).width() ?

我在JSView中有一个函数,如果窗口宽度小于1000,它会执行一些操作。我正在尝试使用Mocha、chai为此编写单元测试,并通过Phantom/Chrome/中的karma测试运行器运行测试Chromium浏览器。我还使用sinon对方法进行stub并使其返回一些所需的值。现在有一个条件检查,如果窗口宽度小于1000,那么我怎么能stub呢,我正在尝试类似下面的方法,sinon.stub($(window).width());$(window).width().returns(900);但它不起作用。有什么特定的方法可以stub$(window).width()值吗?

javascript - 诗乃JS : Is there a way to stub a method on object argument's key value in sinon js

我想在以下响应中模拟对obj.key3值的不同响应。就像ifobj.key3=true然后返回与obj.key3=false不同的响应functionmethod(obj){returnanotherMethod({key1:'val1',key2:obj.key3});} 最佳答案 您可以使用.withArgs()和对象匹配器根据调用它的参数使stub返回(或执行)某些操作。例如:varsinon=require('sinon');//Thisisjustanexample,youcanobviouslystubexistingm

javascript - Sinon.JS 的 stub.callsArg(index) 是做什么的?

说真的,我想不通。文档给了我们:stub.callsArg(index)-使stub调用提供的索引处的参数作为回调函数。stub.callsArg(0);使stub调用第一个参数作为回调。但是,我不知道要索引到的参数列表在哪里。也许我只是不明白什么是stub... 最佳答案 stub是具有可编程行为的noop函数。在您的情况下,callsArg(index)将对stub进行编程以期望在index处有一个函数并立即调用它。functionsayHi(){console.log('hi');}varstub=sinon.stub().c

javascript - 使用 Chutzpah 运行 QUnit (TypeScript) 测试给出 "Called start() outside of a test context while already started"

我有一个相当简单的重现,结果我不明白。确保安装了Chutpah测试适配器4.0.3。使用VisualStudio2013执行以下步骤:创建一个新的.NET4.5.1类库项目;添加NuGet包qunit.TypeScript.DefinitelyTyped0.1.7;将TypeScript文件file1.ts添加到项目中,内容如下:///QUnit.test("QUnitisworking",assert=>assert.ok(true));在该文件内右键单击并从上下文菜单中选择“运行JS测试”。我可以确认file1.js是按预期生成的。结果是没有运行任何测试,测试资源管理器没有显示测试

javascript - '错误 : Unexpected request' during Karma Angular Unit Test

运行gruntkarma时,其中一个指令的测试在尝试获取模板时失败。我使用ng-html2js作为预处理器。这是我的一些karma.conf.jsplugins:['karma-chrome-launcher','karma-jasmine','ng-html2js','karma-ng-html2js-preprocessor'],preprocessors:{'app/scripts/directives/**/*.html':'ng-html2js'},ngHtml2JsPreprocessor:{moduleName:'templates'}在我的测试中,我有以下内容:'use

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