How I used Moles in a real life BizTalk scenario

Background

When you’re building BizTalk applications with TFS Build and then doing some deployment you pretty much end up with the situation where you DO NOT want to create BizTalk applications manually or automatically from some text-file. There is actually a place where this information is stored and it’s called a Bindings File.

A Binding File contains information specific for a BizTalk application and it’s in XML. If you really are into details I encourage you to read this article on MSDN. In short you should use Binding Files and only Binding Files as a part of your automatic deployment process. Thanks to Johan Hedberg and Mikael Håkansson for pointing this out.

Hey this has nothing to do with Moles! Be patient and start by installing Moles here are the free download links from the Visual Studio Code Gallery:

Moles

Moles is a lightweight framework for test stubs and detours in .NET 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 Visual Studio Gallery or bundled with Pex.

That’s what Microsoft Research introduces Moles as so now back to my real life scenario.

Writing extensions to the BindingInfo class with Moles

So there is actually a class in the assembly Microsoft.Biztalk.Deployment.dll that wraps the structure of a Binding File into a .Net class. So my thought was to write some helpful extensions methods that could help me in retrieving the name of the BizTalk application configured in the Binding File.

Are you still with me?

If we take a closer look at the BindingInfo class:

image

The BindingInfo has a default constructor that takes no parameters and then there is a LoadXml method that takes a string to the path of the Binding File. So now I have several options:

  • I could create my own implementation of BindingInfo and create a subclass that I could mock
  • I could use any other Mocking framework, like moq
  • I could use strictly integration tests (tests that use external resources) to test my extension methods

In this case I decided to use Moles because I like to try out new stuff. (Mind you this is not the most common scenario on when to use Moles but just bare with me)

Have you installed it yet? Good lets move on.

Adding a reference to a Moled assembly

Right click any referenced assembly and choose Add Moles Assembly like so:

image

Rebuild your test project and you’ll find two new items added to your project.

image

The first new item is a reference to an assembly that is the Moles representation of (in this case) Microsoft.BizTalk.Deployment.Moles.

And the second new item is a .moles file is a simple XML file telling Moles which Moles representation should be created at build time. This is how my Microsoft.BizTalk.Deployment.moles looks like:

image

This can have implications on build performance that I will try to write another post about soon.

Now you have everything in place to start writing your tests right? Let’s goooo! First of all you’ll need to decorate all your test methods where you’re going to use Moled objects with the HostType attribute. I use my own code snippet for this (based on the TestMethod code snippet already in Visual Studio).

image

And then start writing test cases. But how do you find your Moled objects? Well I usually find my Molable object using the complete namespace to the stuff you want to Mole + Moles + M + intellisense. Look at the example below:

image

If you use complete namespace to the stuff you want to Mole + Moles + S + intellisense then you’ll get the stubbable instances instead like so:

image

In my scenario I wanted to make sure that when the LoadXml method is called on any BindingInfo object in my tests it doesn’t read stuff from my disk but it just returns or initializes any properties I’m interested in. This is how I would write this:

image

A quick recap:

  • MBidingInfo was found using the complete namespace to the stuff you want to Mole + Moles + M + intellisense method mentioned above.
  • AllInstances means that for all instances I want to replace some method with my own delegate.
  • LoadXmlString is the method I want to Mole. After a while using Moles you’ll see that methods have there original name followed by the Type of the input parameters if any.
  • Then I’ve written my delegate declaration and body that takes in two parameters called instance and path.

To make this even more interesting you can add Asserts in the delegate (I say you can, I’m not sure this is a good practice) like the following picture:

image

Now I can continue writing my test as if my BindingInfo objects had loaded the xml from file. But how can I verify that some Moled method has been called? Well Moles doesn’t intend to provide you with Mocking framework abilities that you might be used to. Instead you’ll have to do this yourself. Here is a simple example how to test if a method is called.

image

So this was a real short introduction to Moles that I hope helps you come up to speed quickly. The final point that I would like to make is that you can use this on any .Net object even static or sealed objects like DirectoryInfo for instance (try using the code below without Moles):

image

Big thanks to my colleague Marcus Hammarberg for challenging me to write this post!

Enjoy,

Hugo

Creating a bootable VHD the easy way…about Indexes

I’ve seen a lot of people using and downloading my bat-file from my original post here and some of you have even tried some of my more advanced topics here.

There is something worth mentioning with the attached Install-WindowsImage.ps1 that I’ve received feedback from some of you out there. If you would like to install any other Operating System SKU then the default at Index 1 in my bat-file you’ll have to list the images first.

Let’s take a closer look at Install-WindowsImage.ps1:

image

As you can see someone has already been kind enough to provide us with an easy way to list the available images. So if I go ahead and run the example on my own system I get the following:

image

So imagine if I wanted the DataCenter Edition (Full Installation) instead of Standard (Full Installation) I’d have to change the Index in the provided CreateBootableVHD_v2.bat file:

image

Hope that clear any issues with Indexes you might have.

Enjoy!

Hugo

Building and Unit Testing .Net 4.0 with Team Build 2008, the correct order

I’ve been helping my customer setting up .Net 4.0 build servers lately as you may have read in my previous post. In the previous post, I listed my challenges getting .Net 4 assemblies to build and unit test with Team Build 2008 so I thought It would be time to share the correct order to get the stuff working!

  1. Install Visual Studio 2010 Premium
  2. Install Visual Studio Team System 2008 Development Edition
  3. Install Visual Studio Team System 2008 Service Pack 1
  4. Install Team Build 2008
  5. Install Team Build Service Pack 1
  6. Configured Team Build 2008 to use MSBuild 4.0 instead of MSBuild 3.5. To do this edit %ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\TFSBuildService.exe.config and set the MSBuildPath property to C:\Windows\Microsoft.NET\Framework\v4.0.21006\

That should do it! And if you install your build server in that order you won’t get the following error:

  • MSBUILD : warning : Visual Studio Team System for Software Testers or Visual Studio Team System for Software Developers is required to run tests as part of a Team Build
  • And you won’t replace the Microsoft.TeamFoundation.Build.targets with an incorrect workspace pattern:
    TFS Build 2008:
    $(COMPUTERNAME)_$(BuildDefinitionId)
    After VS 2010 installation:
    $(COMPUTERNAME)_$(BuildDefinitionId)_$(BuildAgentId)

Hope this summarizes how to install a build server that builds and unit tests .Net 4.0 applications with Team Build 2008.

Cheers!

Hugo