<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Ron Jacobs : WF TDD</title><link>http://blogs.msdn.com/rjacobs/archive/tags/WF+TDD/default.aspx</link><description>Tags: WF TDD</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Unit Testing Activities with Windows Workflow Foundation</title><link>http://blogs.msdn.com/rjacobs/archive/2008/08/27/unit-testing-activities-with-windows-workflow-foundation.aspx</link><pubDate>Wed, 27 Aug 2008 21:50:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8901065</guid><dc:creator>ronjacobs</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/rjacobs/comments/8901065.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rjacobs/commentrss.aspx?PostID=8901065</wfw:commentRss><description>&lt;p&gt;The other day somebody asked me about unit testing activities without writing a test workflow.&amp;#160; 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.&amp;#160; I can understand why some people shy away from unit testing workflows because on the surface it looks very difficult.&amp;#160; There are some surprises to be sure but they can all be overcome.&lt;/p&gt;  &lt;p&gt;This morning I took the code that I wrote yesterday and embarked on an ambitious refactoring exercise after getting an email from &lt;a href="http://www.theproblemsolver.nl/"&gt;Maurice de Beijer&lt;/a&gt; who reminded me of the the ServicesExceptionNotHandled event on the runtime that really helps solve a lot of the problems.&lt;/p&gt;  &lt;p&gt;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&lt;/p&gt;  &lt;pre&gt;// Add this handler to rethrow exceptions like failed asserts
workflowTest.Runtime.ServicesExceptionNotHandled += (o, e) =&amp;gt;
{
    throw e.Exception;
};&lt;/pre&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The other issue that Maurice brought up was the fact that often testing a Workflow is more of an integration testing exercise.&amp;#160; He came up with &lt;a href="http://msmvps.com/blogs/theproblemsolver/archive/2008/06/02/unit-testing-custom-workflow-activities.aspx"&gt;some clever ways to unit test&lt;/a&gt; a custom activity using TypeMock.&amp;#160; &lt;/p&gt;

&lt;p&gt;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.&amp;#160; &lt;/p&gt;

&lt;p&gt;If you want to see how I did it, check out the code.&lt;/p&gt;

&lt;p&gt;&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-aabe83468d120033.skydrive.live.com/embedrowdetail.aspx/Samples/Workflow/UnitTestingWorkflowActivities.zip" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8901065" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/rjacobs/archive/tags/WF+TDD/default.aspx">WF TDD</category></item></channel></rss>