0

我正在尝试使用Microsoft.Activities.UnitTesting 对工作流服务进行单元测试目标是模拟服务的扩展,以确保执行所有步骤。

即使扩展已在主机中注册,模拟对象似乎也没有被调用。正如预期的那样,如果未注册扩展,则会引发异常。

        WorkflowServiceTestHost host = null;

        try
        {
            Mock<ISubscriber> publisher = new Mock<ISubscriber>();
            Mock<IWebWorker> webWorker = new Mock<IWebWorker>();

            var voucher = new Voucher();

            using (host = new WorkflowServiceTestHost(workflowServiceFile, serviceAddress))
            {
                host.WorkflowExtensions.Add<ISubscriber>(() => publisher.Object);
                host.WorkflowExtensions.Add<IWebWorker>(() => webWorker.Object);

                host.Open();
                using (var factory = new ChannelFactory<IServiceInterface>(clientBinding, serviceAddress))
                {
                    var proxy = factory.CreateChannel() as IServiceInterface;

                    proxy.Process(voucher);
                }
            }

            **//These validations fail...**

            publisher.Verify(m => m.Push(It.IsAny<Voucher>()), Times.Once(), "ISubscriber.Push was not called.");
            webWorker.Verify(m => m.Done(It.IsAny<Voucher>()), Times.Once(), "IWebWorker.Done was not called.");

            // The host must be closed before asserting tracking
            // Explicitly call host.Close or exit the using block to do this.
        }
        finally
        {
            if (host != null)
            {
                host.Tracking.Trace(TrackingOptions.All);
            }
        }

工作流在 IIS 中按预期运行。

谢谢!

编辑:此错误正在工作流主机输出中写入:

WorkflowInstance "Sequential Service" Unhandled Exception Source "Receive Process Message" 
Exception <System.NotSupportedException: Expression Activity type 'CSharpReference`1' requires compilation in order to run.  
Please ensure that the workflow has been compiled.
at System.Activities.Expressions.CompiledExpressionInvoker.InvokeExpression(ActivityContext activityContext)
at Microsoft.CSharp.Activities.CSharpReference`1.Execute(CodeActivityContext context)
at System.Activities.CodeActivity`1.InternalExecuteInResolutionContext(CodeActivityContext context)
at System.Activities.Runtime.ActivityExecutor.ExecuteInResolutionContext[T](ActivityInstance parentInstance, Activity`1 expressionActivity)
at System.Activities.OutArgument`1.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor)
at System.Activities.RuntimeArgument.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor, Object argumentValueOverride, Location resultLocation, Boolean skipFastPath)
at System.Activities.ActivityInstance.InternalTryPopulateArgumentValueOrScheduleExpression(RuntimeArgument argument, Int32 nextArgumentIndex, ActivityExecutor executor, IDictionary`2 argumentValueOverrides, Location resultLocation, Boolean isDynamicUpdate)
at System.Activities.ActivityInstance.ResolveArguments(ActivityExecutor executor, IDictionary`2 argumentValueOverrides, Location resultLocation, Int32 startIndex)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)>
4

1 回答 1

1

我刚刚意识到WorkflowServiceTestHost是一Microsoft.Activities.UnitTesting门课,而不是你的课。

所以,让我们看看这是否可能。正如我在其源代码中看到的,您可以将 WorkflowService 的对象本身而不是 XAMLX 文件传递​​给构造函数。像这样的东西:

// Load WorkflowService from .xamlx
// Actually this is the method WorkflowserviceTestHost uses when you pass a
// .xamlx so we're taking a step back to be able to compile the body
var wfService = XamlServices.Load("c:\\workflowservice.xamlx") as WorkflowService;

// Compile workflow body
CompileExpressions(wfService.Body);

// Now you can use WorkflowServiceTestHost
using (host = new WorkflowServiceTestHost(wfService, serviceAddress))
{
    // ... do your thing
}

CompileExpressions摘自我之前给你的链接


话虽如此,将 WCF 服务作为单元测试进行测试似乎很奇怪。单元测试应该专注于服务的小活动,这些活动是真正可单元测试的。集成测试(或功能测试)是您测试服务及其所有依赖项(IIS\WAS、网络、数据库等)的地方。

于 2013-05-14T21:58:44.477 回答