Stubbing Redis interactions in javascript using Sinon

后端 未结 5 804
陌清茗
陌清茗 2020-12-16 00:15

I am working in node.js. My app interacts with Redis via the node_redis module. I\'m using mocha and sinon to automate testing of my app. My app looks somethi

5条回答
  •  情歌与酒
    2020-12-16 00:52

    The other way is to do in your class static function getRedis and mock it. For example:

    let redis = {
       createClient: function() {},
    };
    let connection = {
       saddAsync: function() {},
       spopAsync: function() {},
    };
    let saddStub = sinon.stub(connection, 'saddAsync');
    sinon.stub(redis, 'createClient').returns(connection);
    sinon.stub(Redis, 'getRedis').returns(redis);
    
    expect(saddStub).to.be.calledOnce;
    

    Inside your class connect function looks like:

    this.connection = YourClass.getRedis().createClient(port, host, optional);
    

提交回复
热议问题