Testing a WorkflowService .xamlx file with Visual Studio

One thing I have not tackled until today was how to test a Workflow Service in a .xamlx file.  When I started out this morning working on it I realized that there isn’t a great deal of information about how to do this available anywhere.

Update 7/30/2010Download the WF4 Workflow Test Helper library for sample code

Here is what I came up with.  Look at this test and tell me what you think.

 using System.Activities;

using System.Activities.Tracking;

using System.ServiceModel;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using WorkflowTestHelper.Tracking;



namespace WorkflowTestHelper.Tests

{

    /// <summary>

    /// This test demonstrates the WorkflowServiceTestHost class

    /// </summary>

    [TestClass]

    public class WorkflowServiceTestHostTest

    {

        /// <summary>

        /// The endpoint address to be used by the test host

        /// </summary>

        private readonly EndpointAddress _serviceAddress = new EndpointAddress("net.pipe://localhost/TestService");



        /// <summary>

        /// Verifies that the WorkflowServiceTestHost hosts a service and that the service receives and sends a reply

        /// </summary>

        /// <remarks>

        /// Be sure to enable deployment - the xamlx file must be deployed

        /// </remarks>

        [TestMethod]

        [DeploymentItem(@"WorkflowTestHelper.Tests.Activities\TestService.xamlx")]

        public void ShouldHostService()

        {

            var trackingProfile =

                new TrackingProfile

                    {

                        Queries =

                            {

                                new ActivityStateQuery

                                    {

                                        ActivityName = "ReceiveRequest",

                                        States = {"Executing"},

                                    },

                                new ActivityStateQuery

                                    {

                                        ActivityName = "SendResponse",

                                        States = {"Executing"},

                                    },

                            }

                    };





            using (var host = WorkflowServiceTestHost.Open("TestService.xamlx", _serviceAddress.Uri, trackingProfile))

            {

                var client = new ServiceClient(new NetNamedPipeBinding(), _serviceAddress);

                var response = client.GetData(1);

                Assert.AreEqual("1", response);



                host.Tracking.Trace();



                // Find the tracking records for the ReceiveRequest and SendResponse



                // Activity <ReceiveRequest> state is Executing

                AssertTracking.ExistsAt(host.Tracking.Records, 0, "ReceiveRequest", ActivityInstanceState.Executing);



                // Activity <SendResponse> state is Executing

                AssertTracking.ExistsAt(host.Tracking.Records, 1, "SendResponse", ActivityInstanceState.Executing);

            }

        }

    }

}