相关疑难解决方法(0)

如何访问和测试node.js模块中的内部(非导出)功能?

我试图弄清楚如何在nodejs中测试内部(即未导出)函数(最好使用mocha或jasmine).我不知道!

假设我有一个这样的模块:

function exported(i) {
   return notExported(i) + 1;
}

function notExported(i) {
   return i*2;
}

exports.exported = exported;
Run Code Online (Sandbox Code Playgroud)

以下测试(摩卡):

var assert = require('assert'),
    test = require('../modules/core/test');

describe('test', function(){

  describe('#exported(i)', function(){
    it('should return (i*2)+1 for any given i', function(){
      assert.equal(3, test.exported(1));
      assert.equal(5, test.exported(2));
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

有没有办法对notExported函数进行单元测试而不实际导出它,因为它不是要暴露的?

unit-testing mocha.js node.js jasmine

142
推荐指数
4
解决办法
5万
查看次数

标签 统计

jasmine ×1

mocha.js ×1

node.js ×1

unit-testing ×1