如何在scala的specs2测试中使用jUnit的TemporaryFolder? [英] How to use jUnit's TemporaryFolder in scala's specs2 tests?
问题描述
我正在用Playframework写一个测试,我需要创建一个临时文件。
$ b
@RunWith(classOf [JUnitRunner])
class DiagnosticSpec extends Specification {
@Rule $ b $ val temporaryFolder:TemporaryFolder = new TemporaryFolder()
my test在{
val file = temporaryFolder.newFile()// line.35
//继续执行文件
} $ b $ {
运行临时文件b}
}
但是当我运行这个测试的时候, p>
[error] IllegalStateException:临时文件夹尚未创建(MyTest.scala:35)
可以在specs2中使用它吗?如果没有,我怎样才能在specs2中创建一个临时文件,并在测试后自动删除它? 你不能使用JUnit规则与specs2做安装/拆卸。您需要使用 AroundExample
或 FixtureExample
作为:
trait TempFile extends AroundExample {
//这个代码在每个例子的周围执行
def around [R:AsResult] (r)
finally f.delete
类MySpec扩展了TempFile的规范{
test>> {
//在这里使用文件
val file = new File(test)
...
}
}
//或
trait TempFile extends FixtureExample [File] {
//这个代码在每个例子中执行
def fixture [R:AsResult](f:File => R) =
val f = createFile(test)
尝试AsResult(f(r))
finally f.delete
}
class MySpec extends用TempFile指定{
//对于每个测试,文件都可以被注入
test>> {file:File =>
//使用这里的文件
...
}
}
UPDATE
$ b
trait TemporaryFolder extends Specification {
/ **在规范末尾删除临时目录* /
覆盖def map(fs:=> Fragments)= super.map(fs ^ Step(delete))
lazy val tempDir = {
val dir = File.createTempFile(test,)
dir.delete
dir.mkdir
dir
}
/ **在临时目录中创建一个新文件* /
def createNewFile = {
val f = new File(tempDir.getPath +/+ UUID.randomUUID.toString)
f.createNewFile
f
}
/ **删除目录中的每个文件和目录本身* /
def delete = {
选项(tempDir.listFiles).MAP(_。toList).getOrElse(无).foreach(_。delete)
tempDir.delete
}
}
class MySpec将Specification扩展为TemporaryFolder {
test>> ; {
//在这里使用文件
val file = createNewFile
...
}
}
I'm writing a test with Playframework, and I need to create a temporary file.
@RunWith(classOf[JUnitRunner])
class DiagnosticSpec extends Specification {
@Rule
val temporaryFolder: TemporaryFolder = new TemporaryFolder()
"my test" should {
"run with temporary file" in {
val file = temporaryFolder.newFile() // line.35
// go on with the file
}
}
}
But when I run this test, it always throw exception:
[error] IllegalStateException: the temporary folder has not yet been created (MyTest.scala:35)
Is it possible to use it in specs2? If not, how can I create a temporary file in specs2, and delete it automatically after testing?
You cannot use JUnit rules with specs2 to do setup/teardown. You need to use AroundExample
or FixtureExample
for that:
trait TempFile extends AroundExample {
// this code is executed "around" each example
def around[R : AsResult](r: =>Result) =
val f = createFile("test")
try AsResult(r)
finally f.delete
}
class MySpec extends Specification with TempFile {
"test" >> {
// use the file here
val file = new File("test")
...
}
}
// Or
trait TempFile extends FixtureExample[File] {
// this code is executed "around" each example
def fixture[R : AsResult](f: File => R) =
val f = createFile("test")
try AsResult(f(r))
finally f.delete
}
class MySpec extends Specification with TempFile {
// the file can be "injected" for each test
"test" >> { file: File =>
// use the file here
...
}
}
UPDATE
The TemporaryFolder
trait is closer to the original JUnit rule:
trait TemporaryFolder extends Specification {
/** delete the temporary directory at the end of the specification */
override def map(fs: =>Fragments) = super.map(fs ^ Step(delete))
lazy val tempDir = {
val dir = File.createTempFile("test", "")
dir.delete
dir.mkdir
dir
}
/** create a new file in the temp directory */
def createNewFile = {
val f = new File(tempDir.getPath+"/"+UUID.randomUUID.toString)
f.createNewFile
f
}
/** delete each file in the directory and the directory itself */
def delete = {
Option(tempDir.listFiles).map(_.toList).getOrElse(Nil).foreach(_.delete)
tempDir.delete
}
}
class MySpec extends Specification with TemporaryFolder {
"test" >> {
// use the file here
val file = createNewFile
...
}
}
这篇关于如何在scala的specs2测试中使用jUnit的TemporaryFolder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!