Search code examples
unit-testingworkflow-foundation-4workflowservice

How do I assert activity counts in a workflow trace after a unit test?


I am unit testing a WF4 service using the Unit testing Nuget package. There is a functionality for asserting that stuff does or does not exist in the end-of-test tracking information.

Consider the following unit test code:

WorkflowServiceTestHost host = null;
using (host = CreateHost())
{
    var proxy = new ServiceClient(Binding, ServiceAddress);
// Call receive points in the workflow

}
finally
{
    host.Tracking.Trace();
    // I would like to assert stuff like the WF did not abort
    // For now, I would just like to assert that there are a 
    // certain number of invocations of a specific activity.
    // I can find no examples of how to call this:
    host.Tracking.Assert.ExistsCount(?????what goes here???);
}

How does one call ExistsCount()? The prototype looks like this:

public void ExistsCount<TRecord>(Predicate<TRecord> predicate, int count, string message = null) where TRecord : TrackingRecord

but I can find no examples or documentation to understand what is expected for the predicate.


Solution

  • Well, it turns out that the easiest way to handle this is not via

    host.Tracking.Assert.ExistsCount()
    

    rather you want to look at the collection

    host.Tracking.Records
    

    and then use LINQ on them to confirm what you think should be there. For instance if you expected the workflow to call the ABC activity class five times you could check it as follows:

    var trackingRecords = host.Tracking.Records.ToArray();
    var myRecords = trackingRecords.OfType<ActivityStateRecord>()
        .Where(
            r =>
                r.Activity != null && r.State == "Executing" &&
                r.Activity.TypeName.EndsWith("ABC")).ToArray();
    Assert.AreEqual(5,myRecords.Length);