2015-03-26 8 views
8

Akka TestKit ile ScalaTest kullanıyorum, herhangi bir dahili durumu mutasyona uğratmadan başka bir oyuncuya mesaj göndermek için kodladığım bir oyuncu için birleşme ve entegrasyon testleri yazdım. Örneğin bu atın:Başka bir oyuncuya mesaj gönderen Akka aktörünü nasıl test edebilirim?

class MyActor extends Actor { 
    val anotherActor = context.actorOf(Props[AnotherActor]) 
    def receive: Receive = { 
    case MyMessage => anotherActor ! AnotherMessage 
    } 
} 

Ben anotherActorMyActor işleme MyMessage bir sonucu olarak AnotherMessage işlenmiş onaylar bir test yazmak istiyorum.

val testActorRef = TestActorRef(new MyActor) 
"MyActor" should "change some internal state when it receives MyMessage" in { 
    testActorRef ! MyMessage 
    testActorRef.underlyingActor.someState shouldBe "altered" 
} 

Ama benim durumumda böyle devlet umurumda değil de: klasik bir örnek altta yatan aktör almak ve şöyle mesaj aldıktan sonra etkilenmiş gereken bazı iç durumu için kontrol etmek TestActorRef kullanmaktır. Aslında böyle bir durumdan kaçınmak istiyorum. TestProbe.ref'u test altındaki aktöre kaydettirmeniz gerektiğinden, ben de aradığım şey tam olarak değil. Çoğunlukla, testle ilgili Akka dokümantasyonundaki tüm örneklere baktım (http://doc.akka.io/docs/akka/snapshot/scala/testing.html) ama uygun bir şey bulamadım.

cevap

14

Muhtemelen bunu yapmanın birkaç yolu var, size sınava benzer bir şey olduğunda işe yarayan birisini göstereceğim. Hâlâ TestActorRef, TestKit ve TestProbe yollarını düşünüyorum. aşağıdaki yapıyı göz önüne alındığında:

case object MyMessage 
case object AnotherMessage 

class MyActor extends Actor { 
    val anotherActor = createAnother 
    def receive: Receive = { 
    case MyMessage => anotherActor ! AnotherMessage 
    } 

    def createAnother = context.actorOf(Props[AnotherActor]) 
} 

class AnotherActor extends Actor{ 
    def receive = { 
    case _ => 
    } 
} 

sorun size çocuk senin testinde hatta sizin bile bir mesaj alır yapamaz emin gereken bir çocuk aktör ve testin bir parçası olarak yaratan bir aktör örneği olması O çocuğun yaratılmasında herhangi bir kontrol sahibi olmak. Bu durum var, biz şu gibi basit bir şey yapmak (tamamlandı kullanarak specs2 ama ScalaTest benzer bir şey yaratmak mümkün olmalıdır):

import akka.actor._ 
import akka.testkit._ 
import org.specs2.mutable.SpecificationLike 
import org.specs2.runner.JUnitRunner 
import org.junit.runner.RunWith 

class MessageSendingSpec extends TestKit(ActorSystem("test")) with SpecificationLike{ 

    val probe = TestProbe() 
    val myActor = TestActorRef(new MyActor{ 
    override def createAnother = probe.ref 
    }) 


    "Sending MyMessage to an instance of MyActor" should{ 
    "pass AnotherMessage to the child AnotherActor" in { 
     myActor ! MyMessage 
     probe.expectMsg(AnotherMessage) 
     success 
    } 
    } 
} 

aktör oluştururken test etmek olduğunu orada anahtar, ben Probumu sağlamak için çocuğu oluşturan yöntemi geçersiz kılın. Bu ham, aynı zamanda basit ve etkili.

+0

Tam olarak ne arıyordum! TestProbe.ref'de bırakmam gereken bir yöntemle çocuk oyuncuları oluşturmayı bile düşünmedim. Teşekkür ederiz @cmbaxter, – nmurthy

+0

Alma yönteminde yaratılan başka bir aktörümüz varsa bunu nasıl yapıyoruz? –

+0

Bu Java'da nasıl uygulanabilir? –