Microsoft Fakes (formerly Moles)

Problem: You are writing unit tests that involve a dependency on a 3rd party concrete class. Unfortunately there are no interfaces, nor are the methods/properties defined as virtual.

Solution: Microsoft Fakes (formerly Moles)

Microsoft Fakes help you isolate the code you are testing by replacing other parts of the application with stubs or shims. These are small pieces of code that are under the control of your tests. By isolating your code for testing, you know that if the test fails, the cause is there and not somewhere else. Stubs and shims also let you test your code even if other parts of your application are not working yet.

Example:

[code lang=”csharp”]

// Code under test:
public int GetTheCurrentYear()
{
return DateTime.Now.Year;
}

[TestClass] public class TestClass1
{
[TestMethod] public void TestCurrentYear()
{
int fixedYear = 2000;

// Shims can be used only in a ShimsContext:
using (ShimsContext.Create())
{
// Arrange:
// Shim DateTime.Now to return a fixed date:
System.Fakes.ShimDateTime.NowGet =
() =>
{ return new DateTime(fixedYear, 1, 1); };

// Instantiate the component under test:
var componentUnderTest = new MyComponent();

// Act:
int year = componentUnderTest.GetTheCurrentYear();

// Assert:
// This will always be true if the component is working:
Assert.AreEqual(fixedYear, year);
}
}
}
[/code]

Service Bus for Windows Server

Move over MSMQ, Service Bus for Windows Server was recently released.  Apparently it has been for awhile now (Oct 2012 release).  I’m just late to the party I guess.

Service Bus 1.0 was designed to allow for enterprise messaging capabilities in a Windows Server-based environment.  It enables you to build, test, and run loosely-coupled, message-driven applications in self-managed environments and on developer computers.  The two primary features are Service Bus Queues and Service Bus Topics and Subscriptions.

  • Service Bus Queues offer reliable message storage and retrieval with a choice of protocols and APIs. Use Service Bus queue in your application to provide load leveling by having the message receiver processing messages at its own pace or to provide load balancing by having multiple, competing receivers accepting messages from the same queue.

For more information on Service Bus queues refer to How to Use Service Bus Queues.

  • Service Bus Topics and Subscriptions offer (in addition to all of the queue’s) rich publish-subscribe capabilities allowing multiple, concurrent subscribers to independently retrieve filtered or unfiltered views of the published message stream.

If you are interested in or use messaging technology within your applications, this is some pretty exciting news.  Service Bus for Windows Server provides the same capabilities (Message Queues, Pub/Sub) as the Windows Azure Service Bus.  Best of all, it’s free.

It’s available for download now.  Also, check out the MSDN documentation.