激活器的 minimal-akka-scala-seed 的 Specs2 测试有时会失败

Specs2 tests for activator's minimal-akka-scala-seed fails at times

我试图测试乒乓示例(来自激活器的 minimal-akka-scala-seed) using both scalatest,和 specs2(我自己编写了 specs2 测试;见下文)。

最坏的总是过去。但是,specs2 测试并非始终通过(即,如果我多次重新运行相同的测试)。当它失败时,它有以下输出:

   [info] PingPongActorSpecs2
   [info] 
   [info] A Ping actor in specs2 should should
   [error]   ! send back a ping on a pong
   [error]    assertion failed: expected PingMessage(ping), found PongMessage(pong) (TestKit.scala:339)
   [error] akka.testkit.TestKitBase$class.expectMsg_internal(TestKit.scala:339)
   [error] akka.testkit.TestKitBase$class.expectMsg(TestKit.scala:315)
   [error] akka.testkit.TestKit.expectMsg(TestKit.scala:718)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply$$anon$$anonfun.apply(PingPongActor2Spec.scala:27)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply$$anon$$anonfun.apply(PingPongActor2Spec.scala:25)
   [error] akka.testkit.TestKitBase$class.within(TestKit.scala:296)
   [error] akka.testkit.TestKit.within(TestKit.scala:718)
   [error] akka.testkit.TestKitBase$class.within(TestKit.scala:310)
   [error] akka.testkit.TestKit.within(TestKit.scala:718)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply$$anon.<init>(PingPongActor2Spec.scala:25)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply.apply(PingPongActor2Spec.scala:23)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply.apply(PingPongActor2Spec.scala:23)
   [info] 
   [info] A Pong actor in specs2 should should
   [error]   ! send back a pong on a ping
   [error]    assertion failed: expected PongMessage(pong), found PingMessage(ping) (TestKit.scala:339)
   [error] akka.testkit.TestKitBase$class.expectMsg_internal(TestKit.scala:339)
   [error] akka.testkit.TestKitBase$class.expectMsg(TestKit.scala:315)
   [error] akka.testkit.TestKit.expectMsg(TestKit.scala:718)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply$$anon$$anonfun.apply(PingPongActor2Spec.scala:37)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply$$anon$$anonfun.apply(PingPongActor2Spec.scala:35)
   [error] akka.testkit.TestKitBase$class.within(TestKit.scala:296)
   [error] akka.testkit.TestKit.within(TestKit.scala:718)
   [error] akka.testkit.TestKitBase$class.within(TestKit.scala:310)
   [error] akka.testkit.TestKit.within(TestKit.scala:718)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply$$anon.<init>(PingPongActor2Spec.scala:35)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply.apply(PingPongActor2Spec.scala:33)
   [error] com.example.PingPongActorSpecs2$$anonfun$$anonfun$apply.apply(PingPongActor2Spec.scala:33)
   [info] 
   [info] 
   [info] Total for specification PingPongActorSpecs2
   [info] Finished in 19 ms
   [info] 3 examples, 0 failure, 2 errors

我实现的specs2测试如下(与scalatest in the test folder of the minimal-akka-scala-seed非常相似):

 package com.example

 import org.specs2.mutable.SpecificationLike
 import org.specs2.specification.AfterAll

 import akka.actor.{ Actor, ActorSystem, Props }
 import akka.testkit.{ TestKit, ImplicitSender }

 class PingPongActorSpecs2(_system: ActorSystem) extends TestKit(_system) 
   with ImplicitSender 
   with SpecificationLike 
   with AfterAll {
   import scala.concurrent.duration._

   def this() = this(ActorSystem("MySpec"))

   override def afterAll {
     TestKit.shutdownActorSystem(system)
   }

   lazy val waitTime = 1 seconds

   "A Ping actor in specs2 should" >> {
     "send back a ping on a pong" >> {
        val pingActor = system.actorOf(PingActor.props)
        within (waitTime) {
           pingActor ! PongActor.PongMessage("pong")
           expectMsg(PingActor.PingMessage("ping"))
           true
        }
     }
   }

   "A Pong actor in specs2 should" >> {
    "send back a pong on a ping" >> {
      val pongActor = system.actorOf(PongActor.props)
        within (waitTime) {
          pongActor ! PingActor.PingMessage("ping")
          expectMsg(PongActor.PongMessage("pong"))
          true
        }
     }
   }
 }

我的库依赖如下:

 scalaVersion := "2.11.6"

 lazy val akkaVersion = "2.3.13"

 lazy val scalaTestVersion = "2.2.4"

 lazy val specs2Version = "3.6"

 libraryDependencies ++= Seq(
   "com.typesafe.akka" %% "akka-actor" % akkaVersion,
   "com.typesafe.akka" %% "akka-testkit" % akkaVersion % "test",
   "org.scalatest" %% "scalatest" % scalaTestVersion % "test",
   "org.specs2" %% "specs2-core" % specs2Version
 )

有人知道我的 specs2 测试哪里搞砸了吗?

Specs2 规范同时执行示例,因此您的 2 个示例可能会相互影响。您可以为每个示例创建一个系统(昂贵)或使用 sequential 参数让您的测试按顺序执行:

class PingPongActorSpecs2(_system: ActorSystem) extends TestKit(_system) 
   with ImplicitSender 
   with SpecificationLike 
   with AfterAll {

  import scala.concurrent.duration._
  sequential

  ... 
}