我需要断言工作流服务中变量的值。
我已经下载并正在使用 CodePlex 中的 Microsoft.Activities.UnitTesting 框架来测试工作流服务端点、返回值和流逻辑 - 但我需要在调用端点并获得响应后验证变量的值- 那可能吗?
如果不是,是否有其他类型的解决方法可能有效doesn't涉及更改工作流程本身以产生输出参数?因为在生产时我肯定不需要它。
Thanks!
更新2.A
目前使用存根方法而不是 WCF 方法来测试服务。
[TestMethod]
[DeploymentItem(@"TestService\Service1.xamlx")]
public void TestValueOfInteger1AfterStart()
{
// inject the mocks into the service
var xamlInjector = new XamlInjector("Service1.xamlx");
xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));
// setup the messages
var stubExtension = new MessagingStubExtension();
// enqueue a message for the receive activity using parameters content
stubExtension.EnqueueReceive(XName.Get("{http://tempuri.org/}IService"), "Start", null);
// setup the host
var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);
host.Extensions.Add(stubExtension);
try
{
host.TestActivity();
...
更新2.B
因此,经过更多努力,我发现如果使用存根,我可以通过反射恢复上下文,而不是使用 WCF 端点进行单元测试。上面是存根单元测试代码的摘录,下面是我用来获取刷新的 ActivityContext 的反射代码。但是,现在在尝试获取变量的值时出现以下错误。
有趣的是,您可以清楚地看到上下文所绑定的活动是定义它的活动 - 糟糕的框架只是有点混乱。
...
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
// recover the WorkflowInstance
var proxy = stubExtension.GetType().GetProperty("InstanceProxy",
bindingFlags).GetValue(stubExtension,
bindingFlags,
null,
null,
null) as WorkflowInstanceProxy;
// recover the WorkflowInstance
var fieldInfo = proxy.GetType().GetField("instance", bindingFlags);
var workflowInstance = fieldInfo.GetValue(proxy) as WorkflowApplication;
// recover the ActivityExecutor
fieldInfo = workflowInstance.GetType().BaseType.GetField("executor", bindingFlags);
dynamic activityExecutor = fieldInfo.GetValue(workflowInstance);
// recover the rootInstance
fieldInfo = activityExecutor.GetType().GetField("rootInstance", bindingFlags);
var rootInstance = fieldInfo.GetValue(activityExecutor) as ActivityInstance;
// recover the cachedResolutionContext
fieldInfo = activityExecutor.GetType().GetField("cachedResolutionContext", bindingFlags);
var cachedResolutionContext = fieldInfo.GetValue(activityExecutor) as ActivityContext;
MethodInfo methodInfo = cachedResolutionContext.GetType().GetMethod("Reinitialize", bindingFlags);
methodInfo.Invoke(cachedResolutionContext, bindingFlags, null, new object[]
{
rootInstance,
activityExecutor
}, null);
var val = (int)((Sequence)rootInstance.Activity).Variables.First(x => x.Name == "integer1").Get(cachedResolutionContext);
Assert.AreEqual(val, 1, "The integer value of integer1 is not correct.");