我正在尝试使用 Specs2 编写以下规范,但似乎无法使其正常工作,编译器总是抱怨“Unit => org.specs2.execute.Result 没有可用的隐式视图”。
这是测试源:
"generate the correct output when merging an extraction" in {
val source = new File("src/test/resources/sample-docs/text-and-image-on-page-2.pdf")
val output = this.extractor.extract(source)
val pdfGenerator = new ITextPdfGenerator()
val processor = new ExecutableProcessor()
val ocrInput = IOUtils.createTempFile("ocr_input", "pdf")
val ocrOutput = IOUtils.createTempFile("ocr_output", "pdf")
deleteWhenDone[MatchResult[Any]](output.getFullTextFile, ocrInput, ocrOutput) ( {
pdfGenerator.generatePdf(source, ocrInput, output.getPagesWithImages)
processor.process(ocrInput, ocrOutput)
this.extractor.mergeExtraction(output, ocrOutput)
IOUtils.readFile(output.getFullTextFile) === """sample text on line 1 page 1 sample text on line 2 page 1 sample text on line 1 page 3 sample text on line 2 page 3 """
})
}
完成后删除功能如下:
def deleteWhenDone[T]( files : File* ) ( fn : => T ) {
try {
fn
} finally {
IOUtils.deleteFiles( files )
}
}
如果此行位于规范的末尾,它确实有效:
IOUtils.readFile(output.getFullTextFile) === "sample text on line 1 page 1 sample text on line 2 page 1 sample text on line 1 page 3 sample text on line 2 page 3 "
为什么它会像这样失败,我该怎么做才能仍然使用闭包并让测试编译?
编辑
将 deleteWhenDone 调用更改为:
deleteWhenDone[Result](output.getFullTextFile, ocrInput, ocrOutput) ( {
pdfGenerator.generatePdf(source, ocrInput, output.getPagesWithImages)
processor.process(ocrInput, ocrOutput)
this.extractor.mergeExtraction(output, ocrOutput)
IOUtils.readFile(output.getFullTextFile) === "sample text on line 1 page 1 sample text on line 2 page 1 sample text on line 1 page 3 sample text on line 2 page 3 "
})
但还是不行。
编辑 2
感谢 Rafael 的回答,使其工作的最终代码是:
def deleteWhenDone[T]( files : File* ) ( fn : => T ) : T = {
try {
fn
} finally {
IOUtils.deleteFiles( files )
}
}
我错过了方法返回类型。谢谢!