Information to Get started with Unit Testing with Microsoft Fakes and Microsoft Dynamics CRM 2013

This blog will highlight useful information for any CRM developers wanting to learn about Microsoft fakes and Microsoft Fakes with CRM.

I have recently decided I should be unit testing my CRM code, to be honest I knew I should have been doing it before but I had excuses not to

  • The projects I had worked on didn’t have unit tests
  • The other developers were not unit testing their code
  • The company/project didn’t stipulate unit tests should be written
  • Unit testing CRM code is difficult

You can read about this in more detail

Experiences of Unit testing with Microsoft Dynamics CRM Projects

If you want more reasons why CRM developers should write unit tests then read this article it has 18 reasons benefits of unit testing your code

Why CRM Developers should unit test their code

Since I have started unit testing my CRM code not only is my code designed better (smaller classes/methods which are easier to test).  My code is tested and I have caught more bugs, particularly bugs where the testing diverted from the happy path and tested the alternative and exception path tests – Don’t just test the happy path.

The biggest benefit to having unit tests is you can refactor and change the code with confidence because after you have finished making changes you can test your code with unit tests.

What Framework should I use?

I wasn’t sure what framework I should use to test my code.  I had previously worked on a project where they used NUnit, another project used RhinoMocks.

I started writing some unit tests with RhinoMocks but found difficulties when trying to test some code using Metadata because the Metadata was a read only collection.  Microsoft Fakes is an isolation framework which allows you to create shims to overwrite read only data.

CRM 2015 – Understanding CRM Metadata

I had problems setting Attributes collection on the Metadata record.  This was a mixture of me not knowing standard unit testing methods and Attributes is a read only collection.

Many developers were put off using fakes due to cost.  Fakes was free in Visual Studio Ultimate (which is expensive).  The prohibitive cost put off many developers who turned to other free/cheaper testing frameworks.

Sometime in 2013 Microsoft bundled in Microsoft Fakes with Premium license (which is much more common).

Microsoft Fakes is included in Visual Ultimate and Premium in release 2012 and 2013, the key point is Fakes is free with Visual Studio Premium edition.

I had heard good things about Microsoft Fakes, it had shims which would allow me to overwrite read only collections.  So I decided to start testing with Microsoft Fakes.

Start learning about Microsoft Fakes

When I started writing a unit test, I quickly released I was going to have to do some fast learning.  The terms used by Microsoft Fakes were completely alien to me

  • Shims
  • Stubs
  • and no mention of the word Mock
  • Isolation Framework

There are two main hurdles when learning about Microsoft fakes

  • The terminology
  • Getting into the testing mindset
  • Writing tests for the first

To begin with it was tricky to understand the structure and terminology of Microsoft fakes, let alone start testing my tricky CRM code.

Microsoft Fakes Introduction General

This section highlights content I found useful whilst learning about Microsoft Fakes in general without focusing on using Microsoft fakes with to Test CRM code.

A good, quick (7 minutes) video introduction to Microsoft Fakes

Below is a good general article on Unit testing with Visual studio

Writing Unit Tests for the .NET Framework with the Microsoft Unit Test Framework for Managed Code

Microsoft’s main page on Microsoft Fakes

Isolating Code Under Test with Microsoft Fakes

The Isolating Code Under Test with Microsoft fakes is a great introduction to fakes and it explains what Stubs and Shims and gives you some code examples.

Free Microsoft Fakes Ebook

Better Unit Testing with Microsoft Fakes

The ebook above gives a good introduction to Microsoft Fakes but the majority of the ebook is comparing Fakes to other testing frameworks, explaining how to convert your tests/logic from another testing framework to fakes.  It’s useful (and free) but for users getting started with Microsoft fakes you will probably stop reading half way through.

Unit testing with Fakes with Visual studio Premium 2012

This is a good article which is a good introduction to Microsoft fakes with the bonus of the source code being available for download.

CRM and Microsoft Fakes

The best single resource for getting started with writing unit tests using Microsoft Fakes and CRM is a series of 10 blog posts by Zhongchen Zhou

Dynamics CRM 2011 Unit Test Part 1: Introduction and Series Contents

This a fantastic tutorial, going through all the major elements in CRM you need Stub/Shim to enable testing of your CRM code.  Below are links to the fakes CRM testing.  I recommend clicking on the introduction which gives a description of each blog.

The examples are for CRM 2011 but CRM plugin development is based around the IOrganisationService and OrganizationServiceContext and these are still in the same in CRM 2015.

You can also download the source code shown in the tutorials in the link below

Having the code is useful because you can run the tests yourself and walk through the code to follow the unit testing flow (you can also copy parts of it into your unit tests)

Unit Testing Plugins using Microsoft Fakes

A short article which runs through setting up Microsoft fakes, it goes through all the steps to manually test a CRM plugin.  It has useful examples stubbing the IOrganisationService.

OrganizationService.RetrieveMultipleQueryBase = (query) =>
var result = new EntityCollection();
return result;

CRM 2015/2013 Testing Framework

Wael Hamze with help from Ramon Tebar (MVP) have done some great work and created a CRM 2015/2013 testing framework.  This is a framework for testing plugins/custom workflows.  The framework does a lot of mocking of the common plugin objects.

The framework is useful but I think the business logic code should be kept inside plugins.  I prefer to split it up into smaller focused classes and methods but I know a lot of people will be interested in testing the plugins, if so this project will speed up the process.

The project is very well documentated, so you should be up and running in no time.

Not only is this framework excellent but there are some very useful videos showing you how to use the framework

xRM Test Framework for Dynamics CRM 2015

Webcasts and Slides which has videos and presentation on the subject.  There is lots of source

 (Webinar Recording) – CRMUG SIG: Technical/Developer – Unit Testing Plug-ins in Dynamics CRM 2013 (June 2014)

 Technical/Developer – Unit Testing Custom Workflow Activities in Dynamics CRM 2013 (July 2014)

Technical/Developer – Integration Testing Plug-ins in Dynamics CRM 2013 (July 2014)

Books (Unit testing in general)

I’m not qualified to say what books are good but I’m reading and enjoying

The Art of Unit Testing: with Examples in .NET


8 thoughts on “Information to Get started with Unit Testing with Microsoft Fakes and Microsoft Dynamics CRM 2013

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s