【问题标题】:Trying to write a test with a closure in the end fails on Specs2尝试在 Specs2 上最终使用闭包编写测试失败
【发布时间】:2012-01-03 21:20:57
【问题描述】:

我正在尝试使用 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 )
    }
}

我错过了方法返回类型。谢谢!

【问题讨论】:

    标签: scala testing compiler-construction closures specs2


    【解决方案1】:

    deleteWhenDone 函数的结果类型有 Unit,这与 in 参数的预期类型不兼容。您可以通过在定义中添加等号将其更改为返回 T

    def deleteWhenDone[T]( files : File* ) ( fn : => T ) = {
        try {
            fn
        } finally {
            IOUtils.deleteFiles( files )
        }
    }
    

    【讨论】:

      【解决方案2】:

      in方法定义如下:

      def in[T <% Result](r: => T): Example
      

      所以它期望一个代码块作为参数,其返回类型是从 Result 派生的,或者可以隐式转换为 Result。如果您在测试结束时编写 Success(),它将编译。

      你也可以这样做:

      implicit def anyToSuccess[T](a: T): org.specs2.execute.Result = success 
      

      【讨论】:

      • 嗨,Christian,我已将呼叫签名更改为 deleteWhenDone[Result](查看我更新的答案),但仍然无法正常工作。而且我不能调用 Success(),规范结果是文件文本和正在比较的文本之间的 ===。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多