如何在node.js模块中正确导出variables,以使其在unit testing中可见

我想unit testingnode.js模块,我想知道如何正确导出variables和对象? 所以我可以在我的unit testing中访问他们,并在他们的工作。

源代码:

var someObject = {}; var Array = [189, 347, 497, 632, 750, 900, 995, 1137], someNumber = 142, sizeOfArray = Allowance.length; someObject = { method1 : function (info) { //something... return { someOtherMethod1: someObject.method1(info) && someObject.method2(info) someOtherMethod2: someObject.method3(info) }; }, method2 : function (info) { //something... return something2; } method3 : function (info) { //something... return something3; } }; module.exports = someObject; 

所以通过这样做:

 module.exports = someObject; 

我可以在mocha中的unit testing文件中访问someObject,但是如何访问像Array,sizeOfArray,someNumber等其他variables?

我试着添加这个:

 module.exports = { someObject : someObject, Array : Array, someNumber : someNumber, sizeOfArray : sizeOfArray }; 

但是我的unit testing文件不能读取它

编辑:

这是我的unit testing

 var assert = require("assert") var expect = require("expect.js") var chai = require("chai") var sourceCode = require('../src/sourceCode') describe("Something..", function() { var Info = { //some data... }; describe("Properties", function() { it("has someObject defined", function() { expect(someObject).not.to.be.undefined; }); it("has Array defined", function() { expect(Array).not.to.be.undefined; }); it("has someNumber defined", function() { expect(someNumber).not.to.be.undefined; }); it("has sizeOfArray defined", function() { expect(sizeOfArray).not.to.be.undefined; }) }) describe("Methods", function() { it("should have method1 defined", function() { expect(sourceCode.someObject.method1(Info)).not.to.be.undefined; }); it("should have method2 defined", function() { expect(sourceCode.someObject.method2(Info)).not.to.be.undefined; }); }) }; 

@Augusto

这就是我正在谈论的testing方法1

 it("should return positive when conditions are met", function() { var result = sourceCode.someObject.method1(Info); expect(result).not.to.be.undefined; expect(result.eligible).not.to.be.undefined; expect(sourceCode.result.someOtherMethod2).not.to.be.undefined; expect(sourceCode.result.someOtherMethod1).to.equal(true); }); 

我认为这个问题出现在你的unit testing中。 您不访问对象中的属性

 var sourceCode = require('../src/sourceCode'); //CODE CODE CODE expect(Array).not.to.be.undefined; 

在那里你试图访问数组variables,而不是sourceCode.Array属性。 因为你没有定义一个数组variables,你得到了未定义的错误信息。 正确的代码将会是:

 expect(sourceCode.Array).not.to.be.undefined; 

所以..在你的unit testing中试试这个:

 var assert = require("assert") var expect = require("expect.js") var chai = require("chai") var sourceCode = require('../src/sourceCode') describe("Something..", function() { var Info = { //some data... }; describe("Properties", function() { it("has someObject defined", function() { expect(sourceCode.someObject).not.to.be.undefined; }); it("has Array defined", function() { expect(sourceCode.Array).not.to.be.undefined; }); it("has someNumber defined", function() { expect(sourceCode.someNumber).not.to.be.undefined; }); it("has sizeOfArray defined", function() { expect(sourceCode.sizeOfArray).not.to.be.undefined; }) }) };