加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 将Akka TestKit与Specs2一起使用

发布时间:2020-12-16 18:55:49 所属栏目:安全 来源:网络整理
导读:我正在尝试使用Akka的TestKit进行specs2测试.我坚持一个持续的编译错误我无法弄清楚如何解决,我很感激建议. 编译错误是: TaskSpec.scala:40: parents of traits may not have parameters[error] with akka.testkit.TestKit( ActorSystem( "testsystem",Conf
我正在尝试使用Akka的TestKit进行specs2测试.我坚持一个持续的编译错误我无法弄清楚如何解决,我很感激建议.

编译错误是:

TaskSpec.scala:40: parents of traits may not have parameters
[error]   with akka.testkit.TestKit( ActorSystem( "testsystem",ConfigFactory.parseString( TaskSpec.config ) ) )

根据Akka docs和互联网xebia以及Akka in Action的建议,我试图将TestKit纳入specs2 Scope.这是我得到错误的代码片段:

class TaskSpec 
extends Specification 
with AsyncTest
with NoTimeConversions { 

  sequential 

  trait scope 
  extends Scope 
  with TestKit( ActorSystem( "testsystem",ConfigFactory.parseString( TaskSpec.config ) ) ) 
  with AkkaTestSupport {
...

我有以下帮手:

trait AkkaTestSupport extends After { outer: TestKit =>
  override protected def after: Unit = {
    system.shutdown()
    super.after
  }
}

解决方法

这是你可以做的一件事:

import org.specs2.mutable.SpecificationLike
import org.specs2.specification._

class TestSpec extends Actors { isolated
  "test1" >> ok
  "test2" >> ok
}

abstract class Actors extends 
 TestKit(ActorSystem("testsystem",ConfigFactory.parseString(TaskSpec.config)))
 with SpecificationLike with AfterExample {

  override def map(fs: =>Fragments) = super.map(fs) ^ step(system.shutdown,global = true)

  def after = system.shutdown
}

这应该避免你的编译错误,因为TestKit是一个抽象类,它只是混合特征:SpecificationLike是一个特征(规范不是),AfterExample是一个特征.

上面的规范也以隔离模式运行,这意味着为每个示例实例化了一个全新的TestSpec对象,AfterExample特性确保在每个示例之后系统关闭.

最后,使用特殊步骤覆盖map方法,以确保为第一个TestSpec实例(声明所有示例的实例)创建的系统将被彻底处理掉.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读