Monthly Archives: February 2012

Unit Testing for CRM 2011; Plugins, Custom Workflow Activities and Custom .Net Code

Every time we write a plugin and a custom workflow activity, we should make sure it works as desired. The business logic we write within these plugins and custom workflow activities, should accept the business confirmed parameters and should perform the agreed operations.

The above holds true each time we change these plugins and custom workflow activities. Every time we make a change, the plugins and custom workflow activities again need to be tested and verified. Unit testing in Visual Studio 2010 (out of the box) helps us achieve this process. In unit testing we will always need to Mock the CRM contexts and the data.

“Moq” mocking library can help in writing unit tests for CRM 2011 plugins and custom workflow activities, by mocking the CRM context and makes it easier to pass parameters and define the results.

Moq (pronounced “Mock-you” or just “Mock”) is the only mocking library for .NET developed from scratch to take full advantage of .NET 3.5 (i.e. Linq expression trees) and C# 3.0 features (i.e. lambda expressions) that make it the most productive, type-safe and refactoring-friendly mocking library available. And it supports mocking interfaces as well as classes. Its API is extremely simple and straightforward, and doesn’t require any prior knowledge or experience with mocking concepts.

The below screen shot shows the skeleton of how the MOQ can help us in Mocking (the CRM 2011 context). After Mocking, we can unit test by changing input parameters as we do with any other .Net unit testing.
MOQ: Mocking the CRM 2011 Context for Unit Testing in Visual Studio 2010

MOQ: Mocking the CRM 2011 Context for Unit Testing in Visual Studio 2010

I hope this was informative. Please feel free to leave comments, if any.

Advertisements