【问题标题】:Specs2 and Scalacheck - mixing ForEach context with propertiesSpecs2 和 Scalacheck - 将 ForEach 上下文与属性混合
【发布时间】:2019-08-18 05:16:21
【问题描述】:

我正在编写使用来自ScalaCheck 的临时文件和属性的Specs2 测试。没有属性它工作正常:

import better.files.File
import org.specs2.execute.{AsResult, Result}
import org.specs2.mutable.Specification
import org.specs2.specification.ForEach

trait TmpDirContext extends ForEach[File] {
  def foreach[R: AsResult](testWithFile: File => R): Result = {
    val tmpDirCtx = File.temporaryDirectory()
    AsResult(tmpDirCtx.apply(testWithFile))
  }
}


class OkTest extends Specification with TmpDirContext {
  import better.files._
  "Example" should {
    "work" in { tmpDir: File =>
      tmpDir.exists must beTrue
    }
  }
}

val test = new OkTest

specs2.run(test)

如果我添加属性,它不会编译:

import org.scalacheck.Prop
import org.specs2.ScalaCheck

class KoTest extends Specification with ScalaCheck with TmpDirContext {
  "KoTest" should {
    "work" in { tmpDir: File =>
      "for" ! Prop.forAll { value: Int =>
        tmpDir.exists must beTrue
      }
    }
  }
Error:(26, 16) could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[better.files.File => org.specs2.specification.core.Fragment]
"work" in { tmpDir: File =>

我已经设法让它编译,但是测试似乎失败了,因为来自TmpDirContextForEach 已经处理了一个临时文件夹:

class KoTest2 extends Specification with ScalaCheck with TmpDirContext {
  "KoTest2" should {
    "work" >> { tmpDir: File =>
      Prop.forAll { value: Int =>
        tmpDir.exists must beTrue
      }
    }
  }
}

我想我遗漏了一些东西......如何使其工作并在属性测试中使用 tmpDir?

【问题讨论】:

    标签: scala specs2 scalacheck


    【解决方案1】:

    你可以试试下面的方法

    import org.specs2.mutable.Specification
    import java.io.File
    import org.specs2.ScalaCheck
    import org.scalacheck._
    
    class KoTest extends Specification with ScalaCheck with TempDir { sequential
      "KoTest" should {
        "work" >> {
          "for" >> prop { (tmpDir: File, value: Int) =>
            tmpDir.exists must beTrue
          }.after(deleteTmpDir)
        }
      }
    }
    
    trait TempDir {
      implicit def arbitraryTempDir: Arbitrary[File] =
        Arbitrary(tmpDir)
    
      val tmpDir = new File("temp")
    
      def deleteTmpDir = tmpDir.delete
    }
    

    提交here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多