UnitTest WorkflowInstanceID 异常
我正在对 StateMachineWorkflow 进行单元测试,并通过单击测试项目来创建测试方法,然后进行添加 - UnitTest。在项目窗口中,我选择要测试的工作流程及其中的所有方法。
Visual Studio 在我的测试项目中生成了一个测试引用文件夹,其中包含工作流的访问器。它还生成了测试所需的所有 TestMethod()。所有测试方法都使用 MyWorkflow_Accessor target = new MyWorkflow_Accessor()。当我需要调用函数时,我只需执行类似 target.SendEmail() 的操作。
一切正常,除了一件事:我无法使用工作流的 WorkflowInstanceId,当代码到达使用它的行时,它会在工作流中引发异常,“这是一个无效的设计时操作。您只能在运行时执行该操作。” 是否可以通过代码注入WorkflowID?对于这种情况有什么解决办法吗?我在很多函数中使用 WorkflowInstanceId 并更改工作流代码以匹配我的测试似乎不是一个好主意,因为我相信问题出在测试中而不是工作流中。
I am unit testing a StateMachineWorkflow and I create my test methods by clicking in my test project and I make Add - UnitTest. In the project window I select the workflow that I want to test and all the methods in it.
Visual Studio generated a Test Reference folder in my Test Project with an accessor to the workflow. It also generated all the TestMethod() necessary for the testing. All test Methods use a MyWorkflow_Accessor target = new MyWorkflow_Accessor(). When I need to call a function I just do something like target.SendEmail().
Everything works fine, except for one thing: I can't use WorkflowInstanceId of the Workflow, when the code reach a line that uses this it throws an exception in the Workflow, "This is an invalid design time operation. You can only perform the operation at runtime."
Is it possible to inject the WorkflowID by code? Is there any workaround to this situation? I use the WorkflowInstanceId in a lot of functions and changing the Workflow code to match my test doesn't seem like a good idea because I believe the problem is in the test and not in the workflow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的问题中不清楚您是否使用 WF 3.5 或 WF4 进行状态机更新。对于后者,您可以使用 Microsoft.Activities.UnitTesting 来测试工作流。
不过,听起来您正在使用 WF 3.5。如果这是新的开发,我会认真考虑转向 WF4。微软基本上重写了 WF,你越早切换,你的迁移路径就会越容易。
否则,有一些信息 在 MSDN 上使用 WF 3.5 进行测试。
It's not clear from your question if you're using WF 3.5 or WF4 with the state machine update. For the latter, you can use Microsoft.Activities.UnitTesting to test workflows.
It sounds like you're using WF 3.5, though. If this is new development, I would seriously consider moving to WF4. Microsoft basically rewrote WF, and the sooner you switch, the easier your migration path will be.
Otherwise, there is some information on testing with WF 3.5 on MSDN.