Specs2 и Scalacheck - смешивание контекста ForEach со свойствами - PullRequest
0 голосов
/ 27 марта 2019

Я пишу 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
      }
    }
  }
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 =>

Мне удалось его скомпилировать, но затемТест, по-видимому, не проходит, потому что ForEach из TmpDirContext уже удалил временную папку:

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

Полагаю, я что-то упустил ... Как сделать так, чтобы tmpDir был доступен втестирование свойства?

1 Ответ

1 голос
/ 28 марта 2019

Вы можете попробовать следующий подход

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
}

Представлено здесь

...