【问题标题】:How does one mock the createClient method of the node redis module如何模拟节点redis模块的createClient方法
【发布时间】:2019-12-09 02:16:40
【问题描述】:

我试图通过在创建新客户端时使用redis-mock 模块代替redis 模块来避免对正在运行的redis-server 的依赖。到目前为止,我发现无法模拟相关方法:createClient

我已经浏览了sinon documentation on stubsan example run through(在谷歌搜索后找到),并基于这些设置:

一个示例应用

// src/app.js

'use strict';

// import modules
const  express = require('express')
    , bluebird = require('bluebird')
    , redis    = require('redis')
;

// promisify redis
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

// define constants
const app    = express()
    , client = redis.createClient()
    , port   = 3000
;

// set some values
client
  .setAsync('12345', JSON.stringify({vacancyId:12345}))
  .catch(err => console.log(`[ERROR]: error setting value - ${err}`));

// define routes
app.get('/api/vacancy/:vacancyId', (req, res) => {
  client
    .getAsync(req.params.vacancyId)
    .then(val => res.send(val))
    .catch(err => console.log(`[ERROR]: error getting value - ${err}`))
});

// listen on port
app.listen(port);

// export the app
module.exports = app;

及相应的测试

// test/app.js

'use strict';

// import modules
const chai           = require('chai')
    , chaiAsPromised = require('chai-as-promised')
    , chaiHttp       = require('chai-http')
    , redis          = require('redis')
    , redisMock      = require('redis-mock')
    , sinon          = require('sinon')
    , app            = require('../src/app.js')
;

// configure chai
chai.use(chaiAsPromised);
chai.use(chaiHttp);

// define constants
const expect   = chai.expect
    , response = JSON.stringify({vacancyId:12345})
;

// now test
describe.only('App', function() {
  before(function() {
    sinon
      .stub(redis.RedisClient.prototype, 'createClient')
      .callsFake(function() {
        console.log('[TEST]: i never get here :(');
        return redisMock.createClient();
      });
  });

  describe('/api/vacancy/:vacancyId', function() {
    it('should return the expected response', function() {
      return expect(chai.request(app).get('/api/vacancy/12345'))
        .to.eventually
        .have.include({status:200})
        .and
        .nested.include({text:response});
    });
  });
});

我希望测试能够通过(当我删除存根并指向正在运行的 redis 服务器时它会通过):

> scratch-node@1.0.0 test /Users/nonyiah/.src/scratch-node
> mocha --exit



  App
    /api/vacancy/:vacancyId
      ✓ should return the expected response


  1 passing (41ms)

但我收到以下错误:

> scratch-node@1.0.0 test /Users/nonyiah/.src/scratch-node
> mocha --exit



  App
    1) "before all" hook in "App"


  0 passing (10ms)
  1 failing

  1) App
       "before all" hook in "App":
     TypeError: Cannot stub non-existent own property createClient
      at Sandbox.stub (node_modules/sinon/lib/sinon/sandbox.js:308:19)
      at Context.<anonymous> (test/app.js:26:8)



npm ERR! Test failed.  See above for more details.

实现这种模拟的正确方法是什么?

【问题讨论】:

    标签: javascript node.js redis mocking sinon


    【解决方案1】:

    原来我是在加载应用程序后创建存根的。我需要移动应用的实例化:

        // , app            = require('../src/app.js')
    

    创建存根后:

      describe('/api/vacancy/:vacancyId', function() {
        it('should return the expected response', function() {
          let app = require('../src/app.js');
    
          return expect(chai.request(app).get('/api/vacancy/12345'))
            .to.eventually
            .have.include({status:200})
            .and
            .nested.include({text:response});
        });
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 2015-03-31
      • 2023-03-25
      • 2018-12-31
      • 2019-12-04
      • 1970-01-01
      • 2021-04-04
      相关资源
      最近更新 更多