问题
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));
})
})
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?
回答1:
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.
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);
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:
Store your configuration in a template file that won't be modified. This could be called
config_test_template.json
In a
beforeEach
callback, copy this template to a location that will be modified during testing. UsingbeforeEach
ensures that the files used in testing is reset before eachit
callback that belongs to the samedescribe
as thebeforeEach
call.In an
after
callback, delete the file you created for testing.
Something like this:
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
});
});
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.
来源:https://stackoverflow.com/questions/20899459/mocha-preserve-file-state-in-test