Mocha在测试中保留文件状态 [英] Mocha preserve file state in test
问题描述
我有一个单元测试,它正在测试配置文件的更新...当然,运行测试后,我的文件现在已更改.我当时想我可以使用之前"来缓存文件,并在之后"上还原它.
I have a unit test that is testing updating of a config file... Of course after I run the test my file is now altered. I was thinking I could use "before" to cache the file and restore it on "after".
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
)时,它将在我执行它的位置之外的全局安装目录中执行?
However whenever I try to do this it says the file does not exist. It appears that when I run Mocha (-> app $mocha -R spec
), it executes out of the global install directory inside of where I execute it?
有没有一种简单的方法可以实现我想要的?还是我可能只是想解决所有问题?
Is there a easy way to achieve what I want? Or am I maybe just going about it all wrong?
推荐答案
如果从命令行运行mocha,则其当前工作目录将是启动时所在的目录.因此,如果您在项目的顶层目录中,则其当前工作目录将是项目的顶层目录,然后所有相对路径都将相对于项目的顶层目录进行解释.
If you run mocha from the command line, its current working directory is going to be whichever directory you were in when you launched it. So if you are in the top directory of your project, then its current working directory will be the top directory of your project and then all relative paths will be interpreted relative to the top directory of your project.
如果要读取与定义测试的文件相关的文件,可以使用__dirname
并将其与要解释的相对于文件的路径结合起来.这是我在其中一个测试套件中拥有的实际代码:
If you want to read files relative to the file in which your tests are defined you can use __dirname
and join it with paths you want to interpret relative to your file. Here's actual code I have in one of my test suites:
var fs = require("fs");
var path = require("path");
[...]
var spectest_dir = path.join(__dirname, "spectest");
var test_dirs = fs.readdirSync(spectest_dir);
上面的代码相对于我项目的顶层目录位于名为test/spectest.js
的文件中.此代码打开相对于文件所在位置的spectest
目录:也就是说,它将打开名为test/spectest/
的目录,然后处理在该目录中找到的文件以创建要运行的测试列表.
The code above is in a file named test/spectest.js
relative to the top directory of my project. This code opens a spectest
directory relative to where the the file is located: that is, it opens the directory named test/spectest/
and then processes the files it finds there to create a list of tests to run.
话虽如此,如果您在设法将文件恢复到原来的状态时发生错误,则可能会丢失数据.因此,我建议以不同的方式构建测试:
This being said, the way you are doing it you could suffer data loss if an error happens before you manage to restore your file to what it was. So I would recommend structuring your test differently:
-
将您的配置存储在不会被修改的模板文件中.这可以称为
config_test_template.json
在beforeEach
回调中,将此模板复制到测试期间将修改 的位置.使用beforeEach
可以确保在与beforeEach
调用属于同一describe
的每个it
回调之前,重置测试中使用的文件.
In a beforeEach
callback, copy this template to a location that will be modified during testing. Using beforeEach
ensures that the files used in testing is reset before each it
callback that belongs to the same describe
as the beforeEach
call.
在after
回调中,删除为测试创建的文件.
In an after
callback, delete the file you created for testing.
类似这样的东西:
var fs = require("fs");
var path = require("path");
var mod = require('modtotest');
describe('Device Configuration', function(){
var template_path = path.join(__dirname, 'config_test_template.json');
var conf_path = path.join(__dirname, 'config.json');
var config;
describe('Update Config', function(){
beforeEach(function () {
fs.createReadStream(template_path).pipe(fs.createWriteStream(conf_path));
});
after(function () {
fs.unlinkSync(conf_path);
});
it('Should update config', function(done){
mod.updateConfig();
//do assertions
})
// more tests could go here
});
});
注意事项:我尚未运行上面的代码,因此请注意输入错误.这个问题值得寻找复制文件的方法在Node.js中.
Caveats: I've not run the code above so watch out for typos. And this question is worth looking at for ways to copy files in Node.js.
如果以这种方式进行操作,最坏的情况是如果发生故障,则会留下一个额外的文件.没有数据丢失.
If you do it this way, the worst case scenario would be to have an extra file left around if a failure occurs. No data loss.
这篇关于Mocha在测试中保留文件状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!