如何在单元测试后断言工作流跟踪中的 activity 计数?

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

我正在使用 the Unit testing Nuget package 对 WF4 服务进行单元测试。有一个功能可以断言测试结束跟踪信息中存在或不存在东西。

考虑以下单元测试代码:

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???);
}

如何调用ExistsCount()?原型看起来像这样:

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

但我找不到示例或文档来理解 predicate 的预期内容。

好吧,事实证明处理这个问题的最简单方法不是通过

host.Tracking.Assert.ExistsCount()

你更想看看合集

host.Tracking.Records

然后对它们使用 LINQ 来确认您认为应该在那里的内容。例如,如果您希望工作流调用 ABC activity class 五次,您可以按如下方式检查它:

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);