超越特质和自我类型
我想重写 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
}
}
I want to override the ScalaTest trait BeforeAndAfterEach to have that stuff implemented once for all my tests. Finally I got it to compile, but I don't understand why.
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 = ""
}
}
The thing that got it working was the line:
this : org.scalatest.BeforeAndAfterEach with org.scalatest.Suite =>
I found it in the beginning of the BeforeAndAfterEach implementation and copied it.
What does it do, and why do I need it?
Update:
This is a simpler version.
trait MySetup extends FlatSpec with BeforeAndAfterEach {
var service: String = _
override def beforeEach {
service = "apa"
super.beforeEach
}
override def afterEach {
service = ""
super.afterEach
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BeforeAndAfterEach 有一个 Suite 的自类型,这意味着 BeforeAndAfterEach 只能混合到扩展 Suite 的类型中。 ScalaTest 希望您首先选择一个主要套件类型,然后再进行混合行为。
自我类型声明不会在子特征中继承,因此您必须重新声明自我类型。
以下问题在自我类型和子特征之间进行了一些权衡: 自我类型和特质子类之间有什么区别?
有关 ScalaTest 设计的一些背景信息,请参阅:http://www.artima.com/scalazine/articles/selfless_trait_pattern.html
BeforeAndAfterEach has a self-type of Suite, meaning that BeforeAndAfterEach can only be mixed in to a type that extends Suite. ScalaTest wants to you pick a primary suite type first and then mix-in behavior afterwards.
The self-type declaration is not inherited in sub-traits so you have to redeclare the self-type.
The following question has some tradeoffs between self-types and sub-traits: What is the difference between self-types and trait subclasses?
For some background on ScalaTest design, see: http://www.artima.com/scalazine/articles/selfless_trait_pattern.html
我可能编写 MySetup 的方式是这样的:
这样 self 类型的侵入性较小。迈克的回答是正确的。这个想法是允许特征堆叠,这样你就可以根据需要以不同的顺序混合多个像这样的特征。另一篇相关文章是“可堆叠特征模式”:
http://www.artima.com/ scalazine/articles/stackable_trait_pattern.html
您可能还会发现相关 Scaladoc 部分中的示例很有帮助:
http://www.scalatest.org/scaladoc-1.6.1 /org/scalatest/FlatSpec.html#compositionFixtures
注意我也调用 super.afterEach 。为了可堆叠,您需要在 beforeEach 和 afterEach 上调用 super。我尝试这样做,以便如果 super.afterEach 因异常而爆炸,您仍然可以获得此特征的后续行为。 (尽管您的套件可能会在此时中止,因此在这种情况下可能并不重要。但总的来说这是一个好主意。)
The way I'd probably write MySetup is this:
That way the self type is less intrusive. Mike's answer is correct. The idea is to allow traits to stack, so you can mix in multiple traits like this, in different orders if you want. Another relevant article is "The Stackable Trait Pattern":
http://www.artima.com/scalazine/articles/stackable_trait_pattern.html
You may also find the examples in the relevant Scaladoc section helpful:
http://www.scalatest.org/scaladoc-1.6.1/org/scalatest/FlatSpec.html#composingFixtures
Note I call super.afterEach too. To be stackable, you need to call super on both beforeEach and afterEach. I do it in a try so that if the super.afterEach blows up with an exception, you still get the after behavior of this trait. (Though likely your Suite will abort anyway at that point, so in this case it probably doesn't matter. But in general it's a good idea.)
这是用于依赖注入的 Scala 语法。
它的字面意思是
this
特征依赖于
特征。阅读这篇文章了解更多信息。This is Scalas syntax for dependency injection.
It means literally
this
trait depends upon<dependency>
trait. Read this article for more.