gpt4 book ai didi

ScalaTest:BeforeAndAfter 未运行

转载 作者:行者123 更新时间:2023-11-28 20:08:39 25 4
gpt4 key购买 nike

我正在尝试学习如何在 ScalaTest 中使用夹具设置和拆卸。我一直在尝试的一个例子如下:

import org.scalatest._
import scala.collection.mutable

class SampleTest extends FlatSpec with BeforeAndAfter with Matchers{

before {
// Setup code
}

after {
// Teardown code
}

"A Stack" should "pop values in last-in-first-out order" in {
val stack = new mutable.Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}

it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new mutable.Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}

问题在于根本没有执行之前或之后的 block 。我觉得我完全遵循了项目文档中的说明 - 我做错了什么?

最佳答案

我试过你的例子,它运行良好:

import org.scalatest._
import scala.collection.mutable

class SampleSpec extends FlatSpec with BeforeAndAfter with Matchers{

before {
info("Setup code")
}

after {
info("Teardown code")
}

"A Stack" should "pop values in last-in-first-out order" in {
val stack = new mutable.Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}

it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new mutable.Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}

将它粘贴到 REPL 中可以得到:

scala> new SampleSpec execute
SampleSpec:
A Stack
+ Setup code
- should pop values in last-in-first-out order
+ Teardown code
+ Setup code
- should throw NoSuchElementException if an empty stack is popped
+ Teardown code

但是,鉴于您评论说需要说“override def”,我想我知道发生了什么。我认为您的 IDE 可能已将您的代码完成为 BeforeAndAfterEach,即使您想要 BeforeAndAfter。所以你混合了 BeforeAndAfterEach,这确实需要 :override def before...”但是正在查看 BeforeAndAfter 的文档。你能仔细检查一下,看看是否是问题所在?

关于ScalaTest:BeforeAndAfter 未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25777294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com