[英]Node mocha/chai test to see if a folder exists
I am trying to test the creation of a folder of a specific path.我正在尝试测试特定路径文件夹的创建。 I am able to properly check if the folder exists before creating it, but when I run the same check after it's been created it returns undefined.我能够在创建文件夹之前正确检查该文件夹是否存在,但是当我在创建文件夹后运行相同的检查时,它返回未定义。
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();
});
});
the result is:结果是:
1 passing (23ms) 1 failing 1 次通过(23 毫秒)1 次失败
1) Create a folder and check if it's been created check to see if folder is there: AssertionError: expected undefined to be truthy 1)创建一个文件夹并检查它是否已创建检查文件夹是否存在:AssertionError: expected undefined to be truthy
assert.isOk()
doesn't do what you think. assert.isOk()
并没有按照您的想法行事。 It is not an always passing assertion;这并非总是一闪而过的断言; instead, it checks the argument you pass in to see if it's truthy.相反,它会检查您传入的参数以查看它是否真实。 Since you're not passing anything in, the value is undefined
, hence "expected undefined to be truthy."由于您没有传入任何内容,因此值为undefined
,因此“预期 undefined 为真”。
You can just leave off the assertion;您可以放弃断言; the test will pass if there are no failing assertions, so there's no need for a dummy always-passing assertion.如果没有失败的断言,测试将通过,因此不需要虚拟的始终通过的断言。 Or you can pass the result of existsSync
to isOk
:或者您可以将existsSync
的结果传递给isOk
:
it('check to see if folder is there', function() {
assert.isOk(fs.existsSync(dir));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.