Skip to content

Instantly share code, notes, and snippets.

@msell
Created July 17, 2012 14:16
Show Gist options
  • Save msell/3129651 to your computer and use it in GitHub Desktop.
Save msell/3129651 to your computer and use it in GitHub Desktop.
Testing WF4 using MOQ
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Activities.Extensions;
using Microsoft.Activities.UnitTesting;
using Moq;
using NUnit.Framework;
using Rhino.Commons;
using Castle.MicroKernel.Registration;
namespace Workflow.Tests
{
[TestFixture]
public class when_transcoding_to_mpeg_format
{
ITranscodeBurden _transcodeBurden;
IPauseResumeBurden _pauseResumeBurden;
IMpegVideoValidator _mpegVideoValidator;
IMpegTranscoder _transcoder;
ITranscodeRequest _transcodeRequest;
TranscodeRequest _request;
IDictionary<string, object> _result;
[SetUp]
public void set_it_up()
{
TestServiceContainer.Install();
_request = new TranscodeRequest
{
EventIdentity = Guid.NewGuid(),
TranscodeFormat = TranscodeFormat.Mpeg2,
StreamSelections = new int[] { 0, 1 }
};
_transcodeBurden = Mock.Of<ITranscodeBurden>();
_pauseResumeBurden = Mock.Of<IPauseResumeBurden>();
_transcoder = Mock.Of<IMpegTranscoder>();
_mpegVideoValidator = Mock.Of<IMpegVideoValidator>();
var fileAccessUtility = Mock.Of<IFileAccessUtility>();
Mock.Get(fileAccessUtility).Setup(x => x.ContainsFile(It.IsAny<string>())).Returns(true);
// save the reference to the requst that was passed into the transcoder,
// so that we can write assertions against the values of the request
Mock.Get(_transcoder).Setup(x => x.Execute(It.IsAny<ITranscodeRequest>()))
.Callback<ITranscodeRequest>(y => _transcodeRequest = y);
IoC.Container.Register(
Component.For<IFileAccessUtility>().Instance(fileAccessUtility),
Component.For<IMpegTranscoder>().Instance(_transcoder),
Component.For<IMpegVideoValidator>().Instance(_mpegVideoValidator)
);
_result = ExecuteActivity(_request.TranscodeFormat);
}
[Test]
public void it_should_return_a_response()
{
Assert.That(_result.Count, Is.EqualTo(1));
}
[Test]
public void it_should_attempt_to_validate_the_video()
{
Mock.Get(_mpegVideoValidator).Verify(x => x.IsValid(It.IsAny<FileInfo>()), Times.Once());
}
[Test]
public void it_should_derive_the_output_folder_name()
{
Assert.That(_transcodeRequest.OutputFolder, Is.EqualTo(@"TestFiles"));
}
[Test]
public void it_should_derive_the_output_filename()
{
Assert.That(_transcodeRequest.OutputFileName, Is.EqualTo(_request.EventIdentity + ".mpg").IgnoreCase);
}
private IDictionary<string,object> ExecuteActivity(TranscodeFormat format)
{
var activity = new TranscodeActivity();
IDictionary<string, object> result;
var host = new WorkflowInvokerTest(activity)
{
// increase the timeout for debugging tests
DefaultTimeout = new TimeSpan(0, 0, 5, 0)
};
var files = new Stack<string>();
files.Push(@"TestFiles\" + _request.EventIdentity + ".ts");
_request.TranscodeFormat = format;
var inputs = InputDictionary.Create(
"Files", files,
"OverlayDVBS", false,
"Request", _request
);
try
{
host.Extensions.Add(_transcodeBurden);
host.Extensions.Add(_pauseResumeBurden);
// result contains any OutArguments from the workflow
// in the event of transcoding we expect one OutArgument
// named "Result" of type WatchGuard.Export.Transcode.Response
result = host.TestActivity(inputs);
}
finally
{
host.Tracking.Trace();
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment