[英]Node mocha/chai test to see if a folder exists
我正在尝试测试特定路径文件夹的创建。 我能够在创建文件夹之前正确检查该文件夹是否存在,但是当我在创建文件夹后运行相同的检查时,它返回未定义。
var expect = require('chai').expect;
var assert = require('chai').assert;
var fs = require('fs');
var dir = './tmp';
describe('Create a folder and check if it\'s been created', function(){
it('check if folder has been created, if no folder exists, then create one: ', function(done){
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
done();
});
it('check to see if folder is there', function() {
if (fs.existsSync(dir)){
assert.isOk();
}else{
assert.fail();
}
done();
});
});
结果是:
1 次通过(23 毫秒)1 次失败
1)创建一个文件夹并检查它是否已创建检查文件夹是否存在:AssertionError: expected undefined to be truthy
assert.isOk()
并没有按照您的想法行事。 这并非总是一闪而过的断言; 相反,它会检查您传入的参数以查看它是否真实。 由于您没有传入任何内容,因此值为undefined
,因此“预期 undefined 为真”。
您可以放弃断言; 如果没有失败的断言,测试将通过,因此不需要虚拟的始终通过的断言。 或者您可以将existsSync
的结果传递给isOk
:
it('check to see if folder is there', function() {
assert.isOk(fs.existsSync(dir));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.