草庐IT

javascript - 带有 sinon 返回函数的 stub 函数?

我想进行单元测试并覆盖我的代码,这是我的代码,如何用sinon覆盖createClient?constclient=redis.createClient({retry_strategy:function(options){if(options.error){if(options.error.code==='ECONNREFUSED'){returnnewError('Theserverrefusedtheconnection');}if(options.error.code==='ECONNRESET'){returnnewError('Theserverresettheconnect

javascript - 用 Sinon 模拟 Redis 构造函数

我想找出一种在这个模块中模拟redis的方法:constRedis=require('ioredis');constmyFunction={exists:(thingToCheck){letredis_client=newRedis(6379,process.env.REDIS_URL,{connectTimeout:75,dropBufferSupport:true,retryStrategy:functionHere});redis_client.exists(thingToCheck,function(err,resp){//handlingsinhere});}};使用这个测试

javascript - 重置 "called"依靠 Sinon Spy

如何在每次测试前重置Sinonspy的“调用”计数?这是我现在正在做的:beforeEach(function(){this.spied=sinon.spy(Obj.prototype,'spiedMethod');});afterEach(function(){Obj.prototype.spiedMethod.restore();this.spied.reset();});但是当我在测试中检查调用计数时:it('callsthemethodonce',function(){$.publish('event:trigger');expect(this.spied).to.have.b

javascript - 如何使用 sinon stub new Date()?

我想验证各种日期字段是否已正确更新,但我不想在预测newDate()何时被调用时乱七八糟。如何stubDate构造函数?importsinon=require('sinon');importshould=require('should');describe('tests',()=>{varsandbox;varnow=newDate();beforeEach(()=>{sandbox=sinon.sandbox.create();});afterEach(()=>{sandbox.restore();});varnow=newDate();it('setscreate_date',do

javascript - 如何使用 Rollup.js 捆绑使用 Sinon.js 的测试?

我正在使用带有插件rollup-plugin-node-resolve和rollup-plugin-commonjs的Rollup.js来捆绑我使用Sinon.js的测试。当我尝试运行捆绑文件时,出现以下错误:Error:Dynamicrequiresarenotcurrentlysupportedbyrollup-plugin-commonjs是否有任何解决此错误的方法,或者我必须使用Webpack等其他工具? 最佳答案 Sinon.jsv1.x不适用于Rollup.js等捆绑程序。您应该升级到更新的版本(v2.x或更高版本)。

javascript - ES2016 类,Sinon Stub 构造函数

我正在尝试用sinon和es2016消除一个super调用,但我运气不佳。任何想法为什么这不起作用?运行Node6.2.2,这可能是其类/构造函数实现的问题。.babelrc文件:{"presets":["es2016"],"plugins":["transform-es2015-modules-commonjs","transform-async-to-generator"]}测试:importsinonfrom'sinon';classFoo{constructor(message){console.log(message)}}classBarextendsFoo{construc

node.js - 如何使用 sinon stub 用 typescript 编写的类的私有(private)方法

我正在为一个公共(public)方法编写单元测试,该方法反过来调用用typescript(NodeJS)编写的类的私有(private)方法。示例代码classA{constructor(){}publicmethod1(){if(this.method2()){//Dosomething}else{//Dosomethingelse}}privatemethod2(){returntrue;}}现在要测试method1()我需要stubmethod2()这是一个私有(private)方法。这是我正在尝试的:sinon.stub(A.prototype,"method2");Types

node.js - 使用 Sinon-Chai 时测试失败显示 "Error: timeout of 2000ms exceeded"

我正在为以下路线(express)编写集成测试。代码如下:varq=require("q"),request=require("request");/*ExampleofservicewrapperthatmakesHTTPrequest.*/functiongetProducts(){vardeferred=q.defer();request.get({uri:"http://localhost/some-service"},function(e,r,body){deferred.resolve(JSON.parse(body));});returndeferred.promise;

javascript - 使用 Sinon 在 javascript 中 stub Redis 交互

我在node.js中工作。我的应用通过node_redis模块与Redis交互。我正在使用mocha和sinon来自动测试我的应用程序。我的应用看起来像这样:...snipvarredisClient=redis.createClient(redisPort,redisHost);varsomeValue=redisClient.get("someKey");returnsomeValue;....我想stub对redisClient.get()的调用。为此,我还需要对redis.createClient()的调用stub-我认为...这是我的测试代码:...varredis=requ

javascript - 在 sinon 中 stub 整个类以进行测试

序言:我已经阅读了很多SO和博客文章,但没有看到任何可以回答这个特定问题的内容。也许我只是在寻找错误的东西......假设我正在开发一个WidgetManager类,它将对Widget对象进行操作。如何使用sinon来测试WidgetManager是否正确使用了WidgetAPI,而不需要拉入整个Widget库?基本原理:WidgetManager的测试应该与Widget类分离。也许我还没有写Widget,或者Widget是一个外部库。无论哪种方式,我都应该能够测试WidgetManager是否正确使用了Widget的API,而无需创建真正的Widget。我知道sinon模拟只能在现有