【发布时间】:2013-11-25 20:17:27
【问题描述】:
目前我正在为使用nodejs 编写的服务器端代码编写一些单元测试。
现在,我的自定义模块正在使用其他一些模块,我的或来自nodejs 标准库的模块,我想模拟它们。首先,我搜索了一些现有的解决方案,例如我发现:https://github.com/thlorenz/proxyquire 和 https://github.com/mfncooper/mockery。
但今天我尝试使用幼稚的方法并做这样的事情:moduleUnderTest
var fs = require('fs');
exports.foo = function(){
console.log("===foo===");
fs.read();
}
和文件moduleUnderTestSpec:
var fs = require('fs');
var moduleUnderTests = require('../server/moduleUnderTests.js');
fs.read = function(){
console.log("===read===");
}
当我运行grunt jasmine_node 时,我可以看到:
===foo===
===read===
所以在这个简单的例子中,我可以将fs 模块中的一个函数换成另一个。有没有我的方法行不通的情况?
【问题讨论】:
标签: node.js unit-testing mocking