Monday, March 12, 2012

Faking it in VS11 - Moles, the Microsoft mocking/isolation framework, is baked into VS11

Cook Computing - Faking In Visual Studio 11

The Problem

Dealing with Now and why I'm almost done with C# and Java — Karl Seguin's post about how the way you code is a by-product of the progamming language you use — discusses how difficult it is to test unit code like this in C#:

...

Visual Studio 11 Fakes

Some of the comments mention TypeMock Isolator and the Moles project from Microsoft, and it so happens the Visual Studio 11 beta reveals that Moles has been productized into Visual Studio as the Fakes Framework. This can inject two types of dummy implementation into unit tests: stub types for interfaces and overridable methods, and shim types for static and non-overridables methods:

...

Faking DateTime

To test the DateTime code, create a unit test project and right click on one of the referenced assemblies in Solution Explorer. This displays a context menu which has an "Add Fakes Assembly". Select this and two more referenced assemblies are automatically added to the project:

  • Microsoft.QualityTools.Testing.Fakes
  • Microsoft.VisualStudio.QualityTools.UnitTestFramework.10.0.0.0.Fakes.

Visual Studio will automatically generate a file called Microsoft.VisualStudio.QualityTools.UnitTestFramework.fakes in a directory in the project called Fakes. This XML file is used to configure the assembly for which fakes are generated and the namespaces and types that are included. We want to generate a shim type for DateTime so we can change the file to specify the mscorlib assembly:

...

image..."

I've not seen too much talk yet on this, on how VS11 will have an isolation/mocking framework built in and in the box. They've been working on Moles for a while now and it's good to see it make it into the mainstream product, that should help adoption some. Now what I don't know is if you can replace it with others, like you can the unit testing frameworks in VS11?

 

Related Past Post XRef:
Interested in PEX & MOLES? Wish you could go to a five hour workshop on them? Here’s the next best thing…
Think you can’t unit test ASP.Net? Here’s a tutorial for one way, using the power of Pex and Moles

No comments: