gpt4 book ai didi

scalaTest:在 org.scalatest.Status 类型的特征 WordSpecLike 中运行的方法需要 `abstract override' 修饰符

转载 作者:行者123 更新时间:2023-12-04 12:45:39 25 4
gpt4 key购买 nike

我正在尝试为 Akka Actor 编写单元测试。

下面是单元测试代码

import org.scalatest._
import akka.testkit.{TestKit, ImplicitSender, TestActors}
import akka.actor.ActorSystem

class PipFilterTest
extends TestKit(ActorSystem("testSystem"))
with BeforeAndAfterAll
with ImplicitSender
with WordSpecLike {

override def afterAll(): Unit = {
TestKit.shutdownActorSystem(system)
}

"PipeFilter Actor" must {
"send messages if it passes the filter criteria" in {
//test code
}
}
}

当我尝试运行测试代码时出现以下错误 -

sbt:chapter8_structuralPatterns> test
[info] Compiling 1 Scala source to /rajkumar_natarajans_directory/projectName/target/scala-2.12/test-classes ...
[error] /rajkumar_natarajans_directory/projectName/src/test/scala/PipeFilterTest.scala:13:7: overriding method run in trait BeforeAndAfterAll of type (testName: Option[String], args: org.scalatest.Args)org.scalatest.Status;
[error] method run in trait WordSpecLike of type (testName: Option[String], args: org.scalatest.Args)org.scalatest.Status needs `abstract override' modifiers
[error] class PipFilterTest extends TestKit(ActorSystem("testSystem")) with StopSystemAfterAll with ImplicitSender with WordSpecLike {
[error] ^
[error] one error found
[error] (Test / compileIncremental) Compilation failed

当我删除 with BeforeAndAfterAllbeforeAllafterAll 方法时,测试运行正常。但我需要这些来设置测试 Actor 。知道为什么我会收到这些错误。

版本信息:

scalaTest 3.0.5

Akka 2.5.11

斯卡拉 2.12.4

最佳答案

此处使用 scalac 2.12.4、akka 2.5.11、scalaTest 3.0.5 编译和运行:

import org.scalatest._
import akka.testkit.{TestKit, ImplicitSender, TestActors}
import akka.actor.ActorSystem

class PipFilterTest
extends TestKit(ActorSystem("testSystem"))
with WordSpecLike
with ImplicitSender
with BeforeAndAfterAll {

override def afterAll(): Unit = {
TestKit.shutdownActorSystem(system)
}

"PipeFilter Actor" must {
"send messages if it passes the filter criteria" in {
//test code
}
}
}

原因是:

  • WordSpecLike 有run的具体实现
  • BeforeAndAfterAll 只有在基类或前面的特征之一已经实现 run
  • 时才能混入

如果你交换 WordSpecLikeBeforeAndAfterAll,那么编译器认为你想混合 WordSpecLike,但是 BeforeAndAfterAll 那么就没有什么可依赖的了,因为它之前没有实现run的trait,所以整个编译失败。

这实际上非常直观:BeforeAndAfterAllrun 应该看起来有点像这样:

abstract override def run(): CrazyScalatestResultType = {
beforeAll()
super.run()
afterAll()
}

如果没有实现runsuper,那么BeforeAndAfterAll也无法正常运行。

要点:mixins 的顺序很重要。

可能相关的链接:Scala's Stackable Trait Pattern

关于scalaTest:在 org.scalatest.Status 类型的特征 WordSpecLike 中运行的方法需要 `abstract override' 修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49683724/

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