Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Specs2和Scalacheck-将ForEach上下文与属性混合_Scala_Specs2_Scalacheck - Fatal编程技术网

Specs2和Scalacheck-将ForEach上下文与属性混合

Specs2和Scalacheck-将ForEach上下文与属性混合,scala,specs2,scalacheck,Scala,Specs2,Scalacheck,我正在编写Specs2测试,它使用ScalaCheck中的临时文件和属性。如果没有属性,它可以正常工作: 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] {

我正在编写Specs2测试,它使用ScalaCheck中的临时文件和属性。如果没有属性,它可以正常工作:

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
      }
    }
  }
我已设法使其编译,但测试似乎失败了,因为来自TmpDirContext的ForEach已经处理了一个临时文件夹:

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

我想我错过了什么。。。如何使其工作并使tmpDir在属性测试中可用?

您可以尝试以下方法

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
}
提出

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
}