Working with Microsoft Moles Framework

Introduction

Microsoft's PEX and Moles are two frameworks that can generate test suites with high code coverage. While the PEX Framework is an automated White Box Testing tool that can be integrated with Microsoft Visual Studio IDE seamlessly, the Moles Framework is a Microsoft Visual Studio Power Tool that is available as free download and can be used with both Microsoft Visual Studio 2010 and Microsoft Visual Studio 2008. This article presents how this tool can be used in your applications and discusses why it is useful.

Pre-Requisites

To work with Microsoft Moles framework, you should have the one of the following installed in your system:

  • Microsoft Visual Studio 2008 with SP1
  • Microsoft Visual Studio 2010

You can download a copy of Microsoft Moles framework from this link:

To take advantage of the concepts discussed in this article, you should be familiar with all of the following:

  • Microsoft Visual Studio 2010
  • C# programming language
  • .NET Framework
  • Basic knowledge of building, debugging, and testing software applications

Unit Testing, Black Box Testing and White Box Testing

Unit Testing is the act of testing a small unit or piece of code in your application, isolate it from the rest of the application's code and see how it behaves, i.e., if it produces the desired results. Note that each of such units is tested separately. A unit test may also be defined as a small piece of code with assertions that can be used to test the small piece (or unit) in isolation. A unit test essentially comprises of data, method sequence and assertions. You can use unit testing to easily identify the bugs in your application's code and then plan measures to fix them.

White Box and Black Box are two of the widely used types of testing software applications. While Black Box testing involves an external view of the test object, White Box testing is a type of testing that is done by the developer of the code. It requires knowledge of how the code works and how it is structured. In Black Box testing you have lesser code coverage and it doesn't consider all scenarios. In essence, Black Box testing involves testing the functionality of the application as opposed to testing how the internal program structures work. White Box testing has more code coverage and helps you find more potential errors in your application code. If used properly, White Box testing can help you get a more stable build for your application.

What is Moles Framework - Where Does it Fit In?

So, what's the Moles Framework all about? Here's what Microsoft says: "Moles is a lightweight framework for test stubs and detours in the .NET framework that is based on delegates. Moles may be used to detour any .NET method, including non-virtual/static methods in sealed types. Moles is freely available on Microsoft Visual Studio Gallery or bundled with Pex."

Reference: http://research.microsoft.com/en-us/projects/moles/

You can use the Moles framework to have your code isolated from any external dependencies and then test how the code behaves, i.e., whether it produces the desired results. Note that the Moles framework would create the necessary delegates in lieu of the external dependencies your code might need to get executed.

Before you get started using Moles, you should ensure that Moles Isolation Framework for th .NET framework is installed in your system. You can download a copy of this from the link provided at the beginning of this article in the section "Pre-requisites".

Using the Moles Framework with ASP.NET

Let's assume that you have already created a sample ASP.NET project using Microsoft Visual Studio 2010. Let me now run you through the steps you need to follow to get started using the Microsoft Moles Framework for the first time!

Follow these steps to use Microsoft Moles framework in your ASP.NET applications:

  1. Open Microsoft Visual Studio 2010
  2. Click on File-> Add -> New Project -> Test -> Test Project to create a new test project
  3. Add the Microsoft.Moles.Framework and System.Web assemblies to your test project
  4. Select your Test project in the Solution Explorer and click Add -> New item
  5. Select the Moles template named "Moles and Stubs for Testing"
  6. Now, name this Moles Generation Tool definition file as System.Web.moles and click Add

The Moles framework would now automatically generate an assembly with the necessary mocks and stubs of the target assembly (System.Web) and add them to the test project you have created. In essence, whatever your specified target assembly is, you should suffix that with ".moles" when you are naming the Moles Generation Tool definition file. In doing so, the mocks and stubs would be generated for the target assembly automatically.

And, that's it! You are now ready to start writing your unit tests!

Executing Moles on Assemblies from the Command Line

Note that you can also run Moles from the command line on your assemblies. To do this, here are the steps you need to follow:

  • Open the Microsoft Visual Studio 2010 command prompt
  • Navigate to the folder in your system that contains the assemblies you would want Moles to be executed on
  • Now type in the following command from the command prompt:

      Moles.exe <name of the assembly file>
    

That's it! When you do this, the Mole assemblies would be generated as long as the assemblies and their dependent assemblies are available.

Summary

According to Microsoft: "Moles is a lightweight framework for test stubs and detours in .NET that is based on delegates. Moles was designed provide a minimal overhead to the Pex white box analysis, and it encourages programmatic models rather than record/replay tests. Moles may be used to detour any .NET method, including non-virtual/static methods in sealed types." Reference: http://visualstudiogallery.msdn.microsoft.com/en-us/b3b41648-1c21-471f-a2b0-f76d8fb932ee

In using Stubs, Moles and Pex framework from Microsoft, you can easily implement isolated parameterized unit testing in your applications seamlessly. You can use them in conjunction to have Lightweight Test Stubs and Moles for your .NET applications, make your applications easily testable, identify the bugs, fix them and then come up with a bug free code. This article presented a head start to understanding and working with Microsoft Moles framework for building applications that are error free. Happy reading!

Suggested Readings

Here are a few links to that provide further references on this topic:

http://research.microsoft.com/en-us/projects/moles/
http://research.microsoft.com/en-us/projects/pex/documentation.aspx
http://www.softwaretestinghelp.com/white-box-testing/
http://www.codeproject.com/Articles/98369/Mocking-the-Unmockable-Using-Microsoft-Moles-with-.aspx?display=Print





About the Author

Joydip Kanjilal

Microsoft Most Valuable Professional, Author and Speaker. Featured in "MSDN Featured Developer of the Fortnight (India)" a number of times. Winner of Community Credit Awards at www.community-credit.com several times. Authored numerous books and articles in Microsoft .NET and its related technologies. Authored the following books:-- ASP.NET 4.0 Programming (Mc-Graw Hill Publishing) Entity Framework Tutorial (Packt Publishing) Pro Sync Framework (APRESS) Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing) ASP.NET Data Presentation Controls Essentials (Packt Publishing)

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Not long ago, security was viewed as one of the biggest obstacles to widespread adoption of cloud-based deployments for enterprise software solutions. However, the combination of advancing technology and an increasing variety of threats that companies must guard against is rapidly turning the tide. Cloud vendors typically offer a much higher level of data center and virtual system security than most organizations can or will build out on their own. Read this white paper to learn the five ways that cloud …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds