Blog

Choosing the Right Microsoft Dynamics Partner

Unit Testing in CRM 2016 with FakeXrmEasy

Here at Beringer, we take great pride in developing exceptional software to create impactful solutions for our customers. One common practice among developers, is the use of Unit Testing. Testing software allows us to run our application locally and verify it can perform the required tasks before deploying it into an operating environment. This method allows developers to write tests for each specification of the software to check the features of the software, even when a development CRM environment may not be available. Today, I’d like to walk you through setting up Unit Testing for a CRM Custom Workflow Activity using the FakeXrmEasy Framework.

What is FakeXrmEasy?

FakeXrmEasy is a set of libraries that mimic a CRM workflow operating environment. Using the framework allows a developer to write code against a shell CRM system. This code can be used to test software without ever having it interact with a CRM environment.

You can write tests with FakeXrmEasy by creating its “fake” Service Context, then making CRM API calls to it from your code.


var context = new XrmFakedContext();
var service = context.GetFakedOrganizationService();

For each test, load sample data into your Fake CRM Context to use with your Workflow Activity


var context = new XrmFakedContext { ProxyTypesAssembly = Assembly.GetExecutingAssembly() };   
var service = context.GetFakedOrganizationService();

service.Create(new Contact { FirstName = "Jimmy" });

Setting up the Unit Testing Project

To start writing Unit Tests for your code, open your Solution in Visual Studio and add a Unit Test Project.

Once the Testing Project is added, you can run these commands on the NuGet Package Manager Console to install the References for the FakeXrmEasy Framework.


// Install-Package FakeXrmEasy        //For Dynamics CRM 2011
// Install-Package FakeXrmEasy.2013   //For Dynamics CRM 2013
// Install-Package FakeXrmEasy.2015   //For Dynamics CRM 2015
// Install-Package FakeXrmEasy.2016   //For Dynamics CRM 2016

// and this... //
// Install-Package xunit -Version 1.9.2
// Install-Package xunit.runner.visualstudio

With the packages installed, you can start writing tests! Remember to reference your CRM Custom Activity Library and start testing. See the FakeXrmEasy website and CRMUG for some examples.

Thanks for Reading, and may your Unit Testing be a Success!

Beringer Associates, a Microsoft Gold Certified Partner, is always here to provide expert knowledge in topics like these. Please contact us with any questions you may have.

[code-snippet name=”blog”]