简体   繁体   English

node.js从另一个模块所需的模块中获取stub异步函数

[英]node.js proxyquire stub asynchronous function from module required by another module

Module under test: 被测模块:

'use strict';
const config = require('config');
const q      = require('q');

class RedisAccess {
    static getValue(key) {
        let deferred = q.defer();

        if (config.redis.disableInteraction) {
            deferred.resolve();
            return deferred.promise;
        }

        config.redisClient.get(key, function handleResults(err, result) {
        ...
        return deferred.promise;
    }        
}

exports = module.exports = RedisAccess;

Test: 测试:

var proxyquire = require('proxyquire').noPreserveCache();
var assert = require('assert');
var readdirError = new Error('some error');
var redisClientStub = { };
var calledBack;

// Override redisClient used by RedisAccess.js.
var redisClientProxy = proxyquire('../../../lib/data/redis/RedisAccess.js', { 'config' : redisClientStub });

// Test redisClient.get(...) to retrieve value given key using proxyquire for redisClient.
redisClientStub.redisClient.get = function (key, cb) {
    cb(null, 'hello world'); 
};

calledBack = false;

// Test redisClient getValue async function.
redisClientProxy.getValue('some_key', function (err, value) {
    assert.equal(err, null);
    assert.equal('value', 'hello world');
    callback = true;
});

The error when I execute the test is: 执行测试时的错误是:

redisClientStub.redisClient.get = function (key, cb) { ^ redisClientStub.redisClient.get = function(key,cb){^

TypeError: Cannot set property 'get' of undefined TypeError:无法设置未定义的属性'get'

How do I properly stub the config.redisClient.get(...) function? 如何正确存根config.redisClient.get(...)函数?

I figured this out. 我想通了。 I had to put a "stub within a stub" to stub the config.redisClient.get() function: 我必须在存根中放置一个“存根”来存根config.redisClient.get()函数:

// Proxyquire allows unobstrusively overriding dependencies during testing. 
// Override config used by RedisAccess.js.
var configStub = { 
  redisClient : {
    createClient : function (port, address) {
      // redis-mock-js used instead.
    },
    get : function (key, cb) {
      if(key === 'test-rejected') {
        cb(new Error('test-rejected'), 'rejected-promise');
      } 
      else if(key === 'test-true') {
        cb(null, true);
      }
      else if(key === 'test-get-valid') {
        cb(null, 'valid-value');
      }
      else {
        cb(new Error('Should not have gotten here!'), 'value');
      }
    },
  }
};

which allowed me to construct this proxyquire: 这让我构建了这个代理:

var redisAccessProxy = proxyquire('lib/data/redis/RedisAccess.js', { 'config' : configStub });

and run this test using a proxy function for redisClient.get(...) which is called inside of RedisAccess.getValue(...): 并使用redisClient.get(...)的代理函数运行此测试,该函数在RedisAccess.getValue(...)内部调用:

var val = redisAccessProxy.getValue('test-get-valid');
assert.equal(val.isFulfilled(), true);
assert.equal(val.isRejected(), false);
assert.equal(val, 'valid-value');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM