testingakka.net

How to expect some messages and ignore others with akka.net testkit?


I have a test for an actor that might respond with some unexpected messages but eventually it must respond with a particular known message. So in essence I want an assertion that will within in some timespan ignore other messages but expect a known message, like so:

[TestMethod]
[TestCategory("Integration")]
public async Task Should_fetch_fund_shareclass_and_details_from_test_service()
{
    
    var testIsins = new HashSet<string> {"isin1", "isin2", "isin3"};

    var props = Props.Create(() => new DataFetchSupervisor());

    var actor = Sys.ActorOf(props, "fetchSupervisor");
    Within(TimeSpan.FromSeconds(30), () =>
    {
        actor.Tell(new StartDataFetch(testIsins));
        
        //ignore unexpected messages here
        
            var fetchComplteMsg = ExpectMsg<DataFetchComplete>();
        
    });
}

So now this will fail because I get some other messages before DataFetchComplete message.

As always, thanks in advance for any help.


Solution

  • Akka.TestKit class has a number of different versions of ExcpectMsg or equivalent checkers. The one you're looking for is probably a FishForMessage. It takes a predicate and will ignore all incoming messages as long as they will fail to pass predicate's condition. Once a passing message is found this check will complete and your code may be continued.