Updated - May 1, 2013

I've updated the sample to a Visual Studio 2012 solution and created a separate test project (LucasDemoCrm.Test) that contains the NUnit tests. Additionally I have created another test project (LucasDemoCrm.VSTest) that has the same unit tests written to use the Visual Studio Unit Testing Framework. If you are looking for the original VS2010 version, you can download it here: LucasDemoCrm.zip.


This sample contains the solution I used for my three-part blog series on unit testing C# interfaces with Dynamics CRM 2011 using NUnit and Moq. There are three methods in the MockDemo class:

  1. CreateCrmAccount
  2. CreateCrmAccount2
  3. GetPicklistOptionCount

These methods and their corresponding tests show how to unit test CRM interfacing code in progressively more advanced scenarios.

Building the Sample

The sample builds a class library that can then be tested using the NUnit GUI or command-line interface. To build the sample you will need:

  1. The Dynamics CRM 2011 SDK
  2. Moq
  3. NUnit

All three are included in the "references" directory in the solution archive. To execute the tests, see the "Running the test" section toward the end of this article.


Each test in this sample demonstrates how to unit test a Dynamics CRM interface without actually needing to connect to a live CRM system.

  1. The CreateCrmAccount test demonstrates a simple use of object mocks as an introduction.
  2. The CreateCrmAccount2 test demonstrates a more complex use of object mocks as a way to test code that is more realistic.
  3. The GetPicklistOptionCount test demonstrates the use of wrapper classes to unit test interfaces that work with sealed classes.

More Information

A detailed overview of the contents of this sample can be found in the original blog posts:

  1. Part I - Introduction to unit testing, NUnit and Moq
  2. Part II - Testing a real-world CRM interface scenario
  3. Part III - Testing CRM SDK responses that use sealed classes