生成一个无效的文件名称用于testing目的
我有一段我需要testing的代码。 其中一个testing需要添加覆盖,其中stream.Writable
创build给出一个指定的output
文件名。
我需要testingWritable
应该失败的情况,它应该发出error
事件并执行适当的error handling函数等。
现在,我想我只应该创build一个无效的文件,使用一些奇怪的名字。 但是这个任务似乎比预期的更难一些。
我怎样才能自动将这段代码片段放入catch
块?
outputFile = String(outputFile); // not exactly how this is really done, // but the end result is the same try { writer = fs.createWriteStream(outputFile, { flags: 'w' }) .on('error', errorHandler) // basically throws the error .on('close', closeHandler); // function 100% tested } catch (e) { // irrelevant code regarding this question, untested so far }
基本上, outputFile
的价值是什么?
**编辑**
临时testing目录的屏幕截图,从那个有问题的代码创build奇怪的名字。
编辑:
尝试使用真正的长文件名。 (似乎FS只支持长达255-260个字符)
忽略下面的部分
我的解决scheme将是有一个共同的errorHandler,即代码会像…
try{ writer = fs.createWriteStream(outputFile, { flags: 'w' }) .on('error',errorHandler) .on('close', closeHandler); }catch(e){ errorHandler(e); }