I have a fairly simply scenario where I want to do a straight forward integration test. High level overview, is:
- I have an
actor
which responds to a message calledPlaceOrder
- I want to verify this actor
publishes
another message upon receivingPlaceOrder
, in this caseOrderPlaced
The trouble is, for an integration test I can assert that the message has been published via ExpectMsg<OrderPlaced>
. But, I was also expecting it to invoke any actors handling that message too?
Perhaps my understanding of the TestKit
is incorrect here but when inheriting from it you get:
ActorOfAsTestActorRef<T>
ActorOf<T>
Sys.ActorOf(...)
My impression was, ActorOf<T>
and Sys.ActorOf(...)
would behave like a real actor system whereas the ActorOfAsTestActorRef<T>
would be ideal for strictly unit tests and swallow any messages an actor may in turn send.
For example, these are my 2 actors in question:
public class PlaceOrderActor : ReceiveActor
{
public PlaceOrderActor()
{
this.Receive<PlaceOrderMessage>(
message =>
{
this.Handle(message);
});
}
private void Handle(PlaceOrderMessage message)
{
Context.ActorOf(Props.Create<Foo>()).Tell(new OrderPlaced(message.CustomerId, message.OrderItems));
}
}
public class Foo : ReceiveActor
{
public Foo()
{
this.Receive<OrderPlaced>(
m =>
{
});
}
}
My test looks like this. The odd thing I have to orchestrate this integration test myself, i.e. I check that the OrderPlaced
has been published and then explicitly send a message to Foo
:
[TestFixture]
public class IntegrationTest : TestKit
{
[Test]
public void When_Placing_An_Order()
{
var message = new PlaceOrderMessage(
"123",
new List<OrderItem>
{
new OrderItem("Product ABC", 2)
});
this.ActorOfAsTestActorRef<PlaceOrderActor>().Tell(message);
var orderPlaced = this.ExpectMsg<OrderPlaced>();
//if (orderPlaced != null)
//{
//this.ActorOfAsTestActorRef<Foo>().Tell(orderPlaced);
//}
}
}
What I am expecting is, by sending the message PlaceOrder
this should invoke Foo
as it handles OrderPlaced
. I shouldn't need to have the bits commented out in the test?
Can this be done or am I going about this completely wrong?
Thanks in advance, DS.