BehaviorTestKit - hasEffects and expectEffectPF

This commit is contained in:
Christopher Batey 2018-03-27 17:51:54 +01:00
parent 4b54941947
commit b798dfb131
4 changed files with 55 additions and 2 deletions

View file

@ -76,6 +76,16 @@ private[akka] final class BehaviorTestKitImpl[T](_name: String, _initialBehavior
}
}
def expectEffectPF[R](f: PartialFunction[Effect, R]): R = {
ctx.effectQueue.poll() match {
case null throw new AssertionError(s"expected matching effect but no effects were recorded")
case eff if f.isDefinedAt(eff)
f.apply(eff)
case other
throw new AssertionError(s"expected matching effect but got: $other")
}
}
def expectEffectType[E <: Effect](implicit classTag: ClassTag[E]): E =
expectEffectClass(classTag.runtimeClass.asInstanceOf[Class[E]])
@ -114,4 +124,5 @@ private[akka] final class BehaviorTestKitImpl[T](_name: String, _initialBehavior
} catch handleException
}
override def hasEffects(): Boolean = !ctx.effectQueue.isEmpty
}

View file

@ -57,6 +57,11 @@ abstract class BehaviorTestKit[T] {
*/
def getAllEffects(): java.util.List[Effect]
/**
* Returns if there have been any effects.
*/
def hasEffects(): Boolean
/**
* Asserts that the oldest effect is the expectedEffect. Removing it from
* further assertions.

View file

@ -58,6 +58,11 @@ trait BehaviorTestKit[T] {
*/
def retrieveAllEffects(): immutable.Seq[Effect]
/**
* Returns if there have been any effects.
*/
def hasEffects(): Boolean
/**
* Asserts that the oldest effect is the expectedEffect. Removing it from
* further assertions.
@ -68,7 +73,12 @@ trait BehaviorTestKit[T] {
* Asserts that the oldest effect is of type T. Consumes and returns the concrete effect for
* further direct assertions.
*/
def expectEffectType[T <: Effect](implicit classTag: ClassTag[T]): T
def expectEffectType[E <: Effect](implicit classTag: ClassTag[E]): E
/**
* Asserts that the oldest effect matches the given partial function.
*/
def expectEffectPF[R](f: PartialFunction[Effect, R]): R
/**
* The current behavior, can change any time `run` is called

View file

@ -82,7 +82,7 @@ class BehaviorTestKitSpec extends WordSpec with Matchers {
private val props = Props.empty
"BehaviorTeskit" must {
"BehaviorTestKit" must {
"allow assertions on effect type" in {
val testkit = BehaviorTestKit[Father.Command](Father.init())
@ -90,6 +90,33 @@ class BehaviorTestKitSpec extends WordSpec with Matchers {
val spawnAnonymous = testkit.expectEffectType[Effects.SpawnedAnonymous[_]]
spawnAnonymous.props should ===(Props.empty)
}
"return if effects have taken place" in {
val testkit = BehaviorTestKit[Father.Command](Father.init())
testkit.hasEffects() should ===(false)
testkit.run(SpawnAnonymous(1))
testkit.hasEffects() should ===(true)
}
"allow assertions using partial functions - no match" in {
val testkit = BehaviorTestKit[Father.Command](Father.init())
testkit.run(SpawnChildren(1))
val ae = intercept[AssertionError] {
testkit.expectEffectPF {
case SpawnedAnonymous(_, _)
}
}
ae.getMessage should startWith("expected matching effect but got: ")
}
"allow assertions using partial functions - match" in {
val testkit = BehaviorTestKit[Father.Command](Father.init())
testkit.run(SpawnChildren(1))
val childName = testkit.expectEffectPF {
case Spawned(_, name, _) name
}
childName should ===("child0")
}
}
"BehaviorTestkit's spawn" must {