2016-06-26 3 views
0

I次役者コードがあります。俳優の受信方法の中で先物の構成をテストする方法は?

class MyActor @Inject()(dao1: MyDao, dao2: OtherDao, eventBus: EventBus) extends Actor with ActorLogging { 

    import context.dispatcher 
    eventBus.subscribe(context.self, MyTopics.FooTopic) 

    override def receive: Receive = { 
    case Foo(name: String) => { 
     dao.get(name) 
      .flatMap(result => dao2.getSomeMoreStuff(result) 
       .flatMap(data => //do more stuff) 
     ) 
    } 
    } 
} 

は俳優仕上げがfooを処理するとき、それはこの将来の構図を起動すると、この構図が終了する前に、別のFooメッセージを処理するために移動し、OKです(おもう)。

私の問題は、この俳優をテストしている:

class MyActorTest(_system: ActorSystem) extends TestKit(_system) 
with WordSpecLike with Matchers with BeforeAndAfterAll 
with MockitoSugar with DefaultTimeout with ImplicitSender{ 

    def this() = this(ActorSystem("myActorSpec")) 

    override def afterAll { 
    TestKit.shutdownActorSystem(system) 
    } 

    trait MyActorScope extends Scope { 
    val myDao = mock[MyDao] 
    val otherDao = mock[OtherDao] 
    val eventBus = new MyEventBus() 
    val myActor = TestActorRef(new MyActor(myDao, otherDao, eventBus) 
    } 

    "Test" must { 
    "verify dao" in new MyActorScope { 
     when(myDao.get(any)).thenReturn(Future.successful(("myDaoResult"))) 
     when(otherDao.getSomeMoreStuff(any)).thenReturn(Future.successful(("otherDaoResult"))) 
     eventBus.publish(new FooMessage(FooPayload("foo"))) 
     verify(myDao).get(any) 
     verify(otherDao).getSomeMoreStuff(any) 
    } 
    } 
} 

は、だからここに何が起こるか、myDaoが正常に検証されているが、他のダオではないということです。 このメッセージ処理が終わる前に先物の構成が行われなかったからだと思います。

これを処理する方法はありますか?アクターコードは意味がありますか?

ありがとうございます!

+0

を使用して、それを修正しますか? – C4stor

+0

@ C4stor、ありがとう!何の上に?私はメッセージを公開してアクターコードを有効にします。 – Tomer

答えて

0

は、私はあなたがこのケースではアッカawaitAssertを使用することができると思うタイムアウトVerificationMode

"Test" must { 
    "verify dao" in new MyActorScope { 
     when(myDao.get(any)).thenReturn(Future.successful(("myDaoResult"))) 
     when(otherDao.getSomeMoreStuff(any)).thenReturn(Future.successful(("otherDaoResult"))) 
     eventBus.publish(new FooMessage(FooPayload("foo"))) 
     verify(myDao, new Timeout(500, times(1))).get(any) 
     verify(otherDao, new Timeout(500, times(1))).getSomeMoreStuff(any) 
    } 
    } 
関連する問題