我有以下代码,我正在尝试测试。然而proxyquire
找不到readDirectory.js
. 有谁明白为什么?
它返回以下错误,Error: Cannot find module '../readDirectory.js'
并指向行testedModule = proxyquire('../readDirectory.js', {
readDirectory.js
:
var dir = require('node-dir');
var _getFiles = {};
_getFiles._get = function (directory, callback) {
dir.readFiles(directory,
function(err, content, next) {
if (err) throw err;
console.log('content:', content);
});
};
module.exports = _getFiles;
测试:
var mockDir = require('mock-fs');
describe("readDirectory", function () {
var testedModule, callbackSpy, readFileStub;
before(function () {
mockDir({
tmp: {
images: {
thumb_test: "thumbnail pic",
small_test: "small pic",
medium_test: "medium pic"
}
}
});
readFileStub = sinon.stub();
callbackSpy = sinon.spy();
testedModule = proxyquire('../readDirectory.js', {
"node-dir": {
"readFiles": readFileStub
}
});
});
after(function () {
mockDir.restore();
});
it("call readdir with fake directory", function () {
testedModule._get(mockDir, function () {console.log("Hello");});
});
});