Partager via


Unit Testing Activities with Windows Workflow Foundation

The other day somebody asked me about unit testing activities without writing a test workflow.  While it might be possible to unit test some of the business logic that the class does outside of a workflow it seems to me that you need to test these in the context that they will operate it.  I can understand why some people shy away from unit testing workflows because on the surface it looks very difficult.  There are some surprises to be sure but they can all be overcome.

This morning I took the code that I wrote yesterday and embarked on an ambitious refactoring exercise after getting an email from Maurice de Beijer who reminded me of the the ServicesExceptionNotHandled event on the runtime that really helps solve a lot of the problems.

All you have to do to insure that test failures properly bubble up, regardless of where they are thrown, is to add a handler for this event like so

 // Add this handler to rethrow exceptions like failed asserts
workflowTest.Runtime.ServicesExceptionNotHandled += (o, e) =>
{
    throw e.Exception;
};

Now you can do things that I avoided in my code yesterday like fail a test when you receive a WorkflowTerminated event or Assert the output parameters in a WorkflowCompleted event.

The other issue that Maurice brought up was the fact that often testing a Workflow is more of an integration testing exercise.  He came up with some clever ways to unit test a custom activity using TypeMock. 

In my code I decided to go the simple route by creating a new custom activity and then building a test workflow to host it and a unit test to test it. 

If you want to see how I did it, check out the code.

Comments

  • Anonymous
    August 27, 2008
    PingBack from http://hoursfunnywallpaper.cn/?p=3373

  • Anonymous
    December 01, 2008
    I'm very pleased to announce the publication of a new WF 3.5 hands-on lab to the MSDN Virtual Labs site: