Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.IO;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using App.Workflow.Test;
  5. using System.Activities;
  6. using Microsoft.Activities.UnitTesting;
  7. using Microsoft.Activities.UnitTesting.Persistence;
  8. using System.Activities.Statements;
  9.  
  10. namespace App.Tests.Workflow {
  11.  
  12.     public class MockTimerExtension : DurableTimerExtension {
  13.         protected override void OnCancelTimer(Bookmark bookmark) {            
  14.             base.OnCancelTimer(bookmark);
  15.         }
  16.  
  17.         protected override void OnRegisterTimer(TimeSpan timeout, Bookmark bookmark) {
  18.            
  19.             // Here's where you can modify the timespan.  Instead of a more formal mock
  20.             // object I have here you could use a Mocking framework to "advance" time
  21.             // by calculating the difference between DateTime.Now() and when the workflow
  22.             // expects to continue from delay.
  23.  
  24.             // For illustration, I'm intercepting the request to schedule a timer and replacing the
  25.             // timespan with a 15 second timer.
  26.             base.OnRegisterTimer(new TimeSpan(0, 0, 15), bookmark);
  27.         }
  28.     }
  29.  
  30.     [TestClass]
  31.     public class WorkflowTests {
  32.  
  33.  
  34.         // Func<TimerExtension> that returns an instance of our MockTimerExtension
  35.         public TimerExtension GetTimerExtension() {
  36.             return new MockTimerExtension();
  37.         }
  38.        
  39.         [TestMethod]
  40.         public void InitWorkflow() {
  41.  
  42.             // A humble Workflow with a 15 minute delay -- and some Writelines around it
  43.             var _wf = new WF_1();
  44.  
  45.             // The UnitTesting Host from Microsoft.Activities.UnitTesting
  46.             var _host = WorkflowApplicationTest.Create<WF_1>(_wf);
  47.            
  48.             try {
  49.                
  50.                 // Instrumenting the WF Test Application to call a method when a demand for the "TimerExtension" is made.
  51.                 // This works on all the flavors of hosts -- ServiceHost, WFApplication and the UnitTesting versions of these as well.
  52.                 _host.TestWorkflowApplication.Extensions.Add<TimerExtension>(GetTimerExtension);
  53.  
  54.                 // Put the Workflow under Test
  55.                 _host.TestActivity();
  56.                
  57.                 _host.WaitForIdleEvent();
  58.  
  59.                 // Resume
  60.                 _host.TestWorkflowApplication.Run();
  61.  
  62.                 // Wait for completion
  63.                 _host.WaitForUnloadedEvent();
  64.  
  65.             } finally {
  66.                 // Write Output of WF tracing participant
  67.                 _host.Tracking.Trace();
  68.             }
  69.  
  70.         }
  71.     }
  72. }