【发布时间】:2014-01-03 08:23:34
【问题描述】:
我有一个单元测试正在测试配置文件的更新...当然,在我运行测试之后,我的文件现在已更改。我在想我可以使用“之前”来缓存文件并在“之后”恢复它。
mod = require('../modtotest');
describe('Device Configuration', function(){
var confPath = '../config/config.json';
var config;
before(function(){
//cache object
config = require(confPath);
})
describe('Update Config', function(){
it('Should update config', function(done){
mod.updateConfig();
//do assertions
})
});
after(function(){
//restore
fs.writeFileSync(confPath, JSON.stringify(config, null, 4));
})
})
但是,每当我尝试这样做时,它都会说该文件不存在。看来,当我运行 Mocha (-> app $mocha -R spec) 时,它会在我执行它的地方的全局安装目录之外执行?
有没有一种简单的方法可以实现我想要的?还是我可能只是做错了?
【问题讨论】:
标签: javascript node.js testing mocha.js