unit testingnode.js模拟文件系统打开错误

我想提高我的代码覆盖这个function:

function openArchiveFilePath(zipFilepath, next) { var fullyQualifiedZipFilepath = path.resolve(zipFilepath); fs.open(zipFilepath, 'r', openFile); function openFile(err, fd) { if (err) { if (err.code === FS_ERROR_CODE.FILE_NOT_FOUND_ERROR_CODE) { formatErrorMessageAndLog(err, "photo archive file (%s) doesn't exist.", fullyQualifiedZipFilepath); return next(err); } if (err.code === FS_ERROR_CODE.FILE_PERMISSIONS_ERROR_CODE) { formatErrorMessageAndLog(err, "photo archive file (%s) cannot be read, insufficient permissions.", fullyQualifiedZipFilepath); return next(err); } formatErrorMessageAndLog(err, "photo archive file (%s) cannot be open.", fullyQualifiedZipFilepath); return next(err); } return next(null, zipFilepath, fd); } } 

我正在使用摩卡作为testing框架和伊斯坦布尔代码覆盖率。 到目前为止,我可以覆盖的情况下,文件没有find或权限不正确(分别试图打开一个不存在的文件,或打开我从谁删除读取权限创build的文件)。

但是,我怎么可以尝试生成一个更通用的Errorfs.open可以抛出,使代码

  formatErrorMessageAndLog(err, "photo archive file (%s) cannot be open.", fullyQualifiedZipFilepath); return next(err); 

将被覆盖?