scalatestと specs2 の両方を使用して、ping-pong の例 (アクティベーターの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 テストは次のとおりです ( minimal-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
)
spec2 テストで私がどこを台無しにしているか知っている人はいますか?