0

我尝试使用scalatest和 specs2测试乒乓球示例(来自激活器的minimum-akka-scala-seed ) (我自己编写了 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$1$$anonfun$apply$1$$anon$1$$anonfun$2.apply(PingPongActor2Spec.scala:27)
   [error] com.example.PingPongActorSpecs2$$anonfun$1$$anonfun$apply$1$$anon$1$$anonfun$2.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$1$$anonfun$apply$1$$anon$1.<init>(PingPongActor2Spec.scala:25)
   [error] com.example.PingPongActorSpecs2$$anonfun$1$$anonfun$apply$1.apply(PingPongActor2Spec.scala:23)
   [error] com.example.PingPongActorSpecs2$$anonfun$1$$anonfun$apply$1.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$3$$anonfun$apply$2$$anon$2$$anonfun$4.apply(PingPongActor2Spec.scala:37)
   [error] com.example.PingPongActorSpecs2$$anonfun$3$$anonfun$apply$2$$anon$2$$anonfun$4.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$3$$anonfun$apply$2$$anon$2.<init>(PingPongActor2Spec.scala:35)
   [error] com.example.PingPongActorSpecs2$$anonfun$3$$anonfun$apply$2.apply(PingPongActor2Spec.scala:33)
   [error] com.example.PingPongActorSpecs2$$anonfun$3$$anonfun$apply$2.apply(PingPongActor2Spec.scala:33)
   [info] 
   [info] 
   [info] Total for specification PingPongActorSpecs2
   [info] Finished in 19 ms
   [info] 3 examples, 0 failure, 2 errors

我实现的 specs2 测试如下(非常类似于minimum-akka-scala-seed 的测试文件夹中的 scalatest):

 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 测试中搞砸了哪里?

4

1 回答 1

1

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

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

  import scala.concurrent.duration._
  sequential

  ... 
}
于 2016-01-08T03:12:20.073 回答