Did you know Microsoft makes a mocking tool? Meet the latest version of Pex

No? Nor me. (If you are wondering what Mocking is, check out my previous post on Mocking, Stubs and Test Doubles.)

I am at TechEd Europe this week in Berlin, speaking on Entity Framework 4.0 and manning the .NET Framework stand. Behind the .NET Framework stand is the Pex and Code Contracts stand manned by the two lead developers of Pex, Peli de Halleux and Nikolai Tillmann. I looked at Pex many months back but was completely unaware of a great new feature they added in September 2009 – Stubs and Moles. Peli and Nikolai kindly took me through the new features in Pex and also recorded a short podcast which I hope to get published asap on our UK MSDN Podcast on channel9.

Stubs and Moles in Pex gives us:

  • The ability to mock using freely available Microsoft tools. Nice.
  • The ability to mock any .NET method (including static methods). Very, very nice!

The ability to mock any .NET method (i.e. methods you don’t have source code for) is something that few mocking tools can do on the .NET platform. Actually I know of no free tools that do it (please let me know if there are some) and instead I always point people at the commercial tool TypeMock if they need that level of functionality.

Moles in action

This example shows a typical use of moles where we detour the DateTime.Now property getter to our custom delegate.

y2kbug

The Pex Download works with both Visual Studio 2008 and Visual Studio 2010 Beta 2.

As a reminder, Pex is an incubation project for Visual Studio, developed by Microsoft Research, and currently available via http://msdn.microsoft.com/devlabs

This article is part of the GWB Archives. Original Author: Eric Nelson

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.