1

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.

TrueWill
  • 25,132
  • 10
  • 101
  • 150
Brazeta
  • 9
  • 2

1 Answers1

0

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.

TrueWill
  • 25,132
  • 10
  • 101
  • 150
  • Hello, thank you for your answer. I am using WF 3.5. and working with Visual Studio 2010. Moving to WF 4.0 will be hard because the project I am working on is very advanced. I was trying to take advantage of the Accessor that the unit test generate, but so far I have no luck because I can access the functions inside de workflow but the thing like WorkflowInstanceID dont work when I call them by the accessor. – Brazeta Nov 02 '11 at 13:18
  • Sorry, I don't have any experience with WF 3.5. Perhaps someone else can suggest a solution. – TrueWill Nov 02 '11 at 14:36