Friday, June 28, 2013

[Visual C#] How to test your applications using the Fakes Framework (1/3)

As already mentioned on my blog multiple times, one of the ways of having good quality code is to use the Test Driven Development (TDD) approach. I have shown you how to apply the TDD using Visual Studio 2012 in a tutorial on my blog here.

When talking about TDD we have to talk about unit tests. Testing your code correctly can be very complicated sometimes and even nearly impossible to achieve. This is especially true for legacy code, which might not adhere to best practices such as loosely coupling and abstraction.

To help you with these kind of problems, Microsoft has created the Fakes Framework. It will aid you to create good unit tests for new code that you write (via Stubs) but also for legacy code (via Shims).

This blog series aims at presenting you the benefits of using the Fakes Framework. It can also be used as tutorial to start using it in your daily work.

What is a good unit test ? How to handle coupling to external sources ?

One of the main problems when writing unit tests is the dependency on external sources such as databases, external components, web services and even method calls within the same class.

For example, if you write a unit test that queries a database, how to handle data inconsistencies or database unavailability ? The unit test will fail in these cases, even if the code is implemented in an efficient and correct way ! This is due to the fact that the unit tests does not test the code in an atomic way, but instead it tests the code as well as all of its external dependencies. But conceiving and implementing atomic unit tests absolutely necessary to assure consistent and correct test results.

So how do we create unit tests that are independent from external sources ? We apply mocking ! This means replacing the real external sources by fake sources (also called Shims or Mocks) within your code. You do connect to the database anymore during your unit tests, for example. You substitute and mock the data objects (such as POCO, DataSet, DataTable, etc…) containing the database query results and your unit tests become decoupled.

We have solved one important problem, but we have added another since we need now to be able to create mock objects easily and quickly. Your unit tests will have better quality but if mock creation takes too much time it will have a negative impact on developer efficiency. The Fakes Framework allows for the automatic creation of mock objects, thus eliminating the cited problem.

What is the Fakes Framework ?

The Fakes Framework is the next generation of the Moles & Stubs project, which was created by Microsoft Research. Please note that the Moles & Stubs project is not supported in Visual Studio 2012 anymore, but you may very well use it in Visual Studio 2010 of course. The Fakes Framework is only included in Visual Studio 2012 Ultimate and you have to modify your existing Moles & Stubs code since it is not compatible out of the box. The basic functionalities are the same but nonetheless much has been improved and modified. The Moles are now called Shims for example !

By using the Fakes Framework development teams can create quickly and efficiently unit tests. Two different approaches are provided: Stubs and Shims.

Stubs are substitute objects that help to isolate the code that needs to be tested. To be able to use them correctly, you have to have loosely coupled classes in your code. This can be achieved by using interfaces for example. Stubs are very well adapted to new code.

Shims are very different, because they allow the interception of method calls during runtime. This allows mocking objects and function calls , which could normally not be done. Shims are very well adapted to legacy code.

Here is a comparison between Stubs and Shims and their provided functionalities:




Interception Mechanism

Virtual Methods

Runtime Instrumentation

Static Methods / Sealed



Internal Types



Private Methods



Static Constructor / Finalizers









Abstract Methods




Thursday, June 27, 2013

[.NET 4.5.1 - C#] New features in the .NET 4.5.1 and Visual Studio 2013 Preview

The next version of the .NET Framework is .NET 4.5.1. It has been published as Preview version as announced yesterday during the BUILD conference. This blog posts aims to present what has been added, modified and improved in this new Framework version.

The .NET Framework 4.5.1 Preview is the first update of .NET Framework 4.5. It contains critical fixes, improvements, and opt-in features and is part of the Visual Studio 2013 and Windows 8.1 Previews. But it is also available as direct download without the requirement of having an existing .NET Framework 4.5 installation.

Visual Studio 2013 and .NET 4.5.1 language based improvements

The following language based improvements that have been added to the Visual Studio 2013 and .NET 4.5.1 Previews :

  • X64 edit and continue is now possible (with some restrictions, but now X86 and X64 are nearly handled the same, which is good news)
  • Async/Await debugging has been added (such as support in the Call Stack window, Tasks window, etc…)
  • Managed return value inspection has been added (in the Autos and Watch windows it allows for easier return value debugging)
  • Windows Store application development improvements (compatibility with Windows 8.1, Win RT improvements, etc…)
  • ADO.NET idle connection resiliency has been added (rebuild broken idle connections with SQL databases automatically and transparently)
  • ASP.NET application suspension has been added (Idle sites are suspended from CPU activity and are paged to disk)
  • On-demand large object heap compaction (instruct the Garbage Collector to compact the large object heap, as part of the natural GC or a forced GC)
  • Multi-core JIT improvements (support for dynamically loaded assemblies)
  • Consistent performance after .NET Framework updates (application startup performance will be more consistent after a .NET Framework update)

Installation of Visual Studio 2013 and .NET 4.5.1

The installation of the .NET 4.5.1 Preview can be done by installing the Visual Studio 2013 Preview in which case it will be automatically installed with the Visual Studio components. Note that it will replace existing .NET 4.5 installations since it is an in-place upgrade.

  • Download the ISO for Visual Studio 2013 Ultimate Preview from here (you may also install any of the other version available if needed).


  • After downloading the ISO, you may now start the installation. The installation will require a high amount of empty hard disk space (in my example 14GB !!).

Install VS 2013    Install VS 2013 2

You may also just install the in-place upgrade directly on your test server without the full Visual Studio development environment. You can download it from here.

You are now ready to start you evaluation of the new features within Visual Studio 2013 Preview and .NET 4.5.1 Preview !

Future language upgrades now also possible via NuGet

Future language updates will be shipped via upgrades and service packs as it is already done today. They will also be available via component specific NuGet packages. This allows for a maximum of flexibility concerning your upgrade strategy. If you do not want to wait until language upgrades are fully available, you may partially upgrade your language components via NuGet packages depending on your specific customer needs.

Furthermore, you can now retrieve NuGet packages for the .NET Framework 4.5.1 and above on Windows Update (it will not work for earlier versions of the .NET Framework). This provides are very easy and efficient way of getting the latest upgrades for the packages that you use. Note that upgrades will only be serviced for NuGet packages that are actually used by your applications.


The .NET 4.5.1 Preview Framework update provides some fixes and multiple  performance enhancements. There are no major language features, but nonetheless those upgrades become very handy and will allow for a more seamless and productive software development experience. I think that the next big language features that we can expect will be around Roslyn, when it will finally be finished and shipped as RTM version. But we will see !


[VS2013 - .NET 4.5.1] Preview of VS2013, TFS 2013, .NET 4.5.1 and Windows 8.1 annouced during Microsoft's BUILD conference

Yesterday during the BUILD conference in San Francisco one of the main major news was the announcement of multiple new product versions for you preferred development platform and operation system (Visual Studio 2013, Team Foundation Server 2013, .NET 4.5.1 and Windows 8.1). 

They are in Preview state and you can download them from here:

Those are preview version, which means that there is no support and that the final version could still be modified. In the past we have already seen that APIs have been changed and new functionalities been added to final versions.So you should use them for educational purpose only.

I have installed those versions in a VM to be able to test them and to give you some feedback on my blog. I am going to show you some of the new features in action in the next days/weeks. So stay tuned if you are interested in this topic.