我想覆盖 ScalaTest 特征 BeforeAndAfterEach 以便为我的所有测试实现一次。最后我得到了编译,但我不明白为什么。
trait MySetup extends BeforeAndAfterEach {
this : org.scalatest.BeforeAndAfterEach with org.scalatest.Suite =>
var service: String = _
abstract override def beforeEach(): Unit = {
service = "apa"
super.beforeEach()
}
abstract override def afterEach(): Unit = {
service = ""
}
}
让它工作的是这条线:
this : org.scalatest.BeforeAndAfterEach with org.scalatest.Suite =>
我在 BeforeAndAfterEach 实现的开头找到了它并复制了它。
它有什么作用,我为什么需要它?
更新:
这是一个更简单的版本。
trait MySetup extends FlatSpec with BeforeAndAfterEach {
var service: String = _
override def beforeEach {
service = "apa"
super.beforeEach
}
override def afterEach {
service = ""
super.afterEach
}
}