using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using App.Workflow.Test;
using System.Activities;
using Microsoft.Activities.UnitTesting;
using Microsoft.Activities.UnitTesting.Persistence;
using System.Activities.Statements;
namespace App.Tests.Workflow {
public class MockTimerExtension : DurableTimerExtension {
protected override void OnCancelTimer(Bookmark bookmark) {
base.OnCancelTimer(bookmark);
}
protected override void OnRegisterTimer(TimeSpan timeout, Bookmark bookmark) {
// Here's where you can modify the timespan. Instead of a more formal mock
// object I have here you could use a Mocking framework to "advance" time
// by calculating the difference between DateTime.Now() and when the workflow
// expects to continue from delay.
// For illustration, I'm intercepting the request to schedule a timer and replacing the
// timespan with a 15 second timer.
base.OnRegisterTimer(new TimeSpan(0, 0, 15), bookmark);
}
}
[TestClass]
public class WorkflowTests {
// Func<TimerExtension> that returns an instance of our MockTimerExtension
public TimerExtension GetTimerExtension() {
return new MockTimerExtension();
}
[TestMethod]
public void InitWorkflow() {
// A humble Workflow with a 15 minute delay -- and some Writelines around it
var _wf = new WF_1();
// The UnitTesting Host from Microsoft.Activities.UnitTesting
var _host = WorkflowApplicationTest.Create<WF_1>(_wf);
try {
// Instrumenting the WF Test Application to call a method when a demand for the "TimerExtension" is made.
// This works on all the flavors of hosts -- ServiceHost, WFApplication and the UnitTesting versions of these as well.
_host.TestWorkflowApplication.Extensions.Add<TimerExtension>(GetTimerExtension);
// Put the Workflow under Test
_host.TestActivity();
_host.WaitForIdleEvent();
// Resume
_host.TestWorkflowApplication.Run();
// Wait for completion
_host.WaitForUnloadedEvent();
} finally {
// Write Output of WF tracing participant
_host.Tracking.Trace();
}
}
}
}