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:

Functionality

Stubs

Shims

Interception Mechanism

Virtual Methods

Runtime Instrumentation

Static Methods / Sealed

No

Yes

Internal Types

Yes

No

Private Methods

No

Yes

Static Constructor / Finalizers

No

Yes

Performance

Good

Low

Deployment

Xcopy

Installation

Abstract Methods

Yes

No


Share/Save/Bookmark

No comments: