UNIT TESTING D365 USING FAKE XRM EASY

April 23, 2019

Author: Rajiv Sapkota 

Introduction to FakeXRMEasy 

FakeXrmEasy is an open source automated testing framework and a mocking library designed for Dynamic CRM projects version 2011, 2013, 2015 and 2016. It helps by giving us mocked objects like an Organization Service, or a plug-in context, or a code activity context, which use the in-memory context. It is more precisely, a mocking library for Dynamics CRM which we can use with our test framework of choice (XUnit, NUnit, etc.).  It is like having an Organization Database, but much more light weight. It allows us to develop and run tests straight from Visual Studio or MS Build without even opening hundreds of CRM windows and forms, therefore, making development life cycle much more efficient. FakeXrmEasy enhances organization to be more productive because it uses Memory organization database, where we can run hundreds of automated tests in seconds. 

There are other automated testing framework for Dynamics CRM projects as well, but they rely on mocking libraries like Microsoft Fakes, where, we have to code our own mocks from the scratch which is really time consuming. So, FakeXrmEasy goes further than that as it provides us with already mocked Organization Service which intercepts our plugin, code activities and so on. The point is, you don’t have to worry about mocking anything, it’s already done for you! 

FakeXrmEasy provides runnable implementations of the common CRM standard organization messages out of the box. Common messages like Create, Retrieve, RetrieveMultiple, Update and Delete and therefore provides kind of a “virtual” mini CRM system including things like an organization message processing pipeline which behaves like “real” CRM where all organization messages are executed. 

More details about FakeXRMEasy can be found at https://dynamicsvalue.com/home

How can we use FakeXrmEasy to Unit Test Dynamics 365 Plugins and Code Activities? 

Creating a sample plugin 

Welcome to your first unit testing experience with FaxeXrmEasy! For this we are going to create a scenario where we will write a plugin on create and update message of the Quote line where when the quote line is created/updated we make sure to alter the sales price by multiplying it with 1.6 to priceperunit and modifying ispriceoverridden to true. 

The sample of the plugin is illustrated below. 

  1. Creating the test project 

After the plugin is ready now let’s test this using FakeXrmEasy, for this Create a unit test project for your plugin which should have all the necessary packages of FakeXrmEasy. This can be done by either of the following two ways: 

a) By Using JLattimer D365 visual studio extension to create a test project. This will fetch all necessary FakeXrmEasy packages required to test the plugin. 

b) By installing them via package manager console using following commands: 

//  Install-Package FakeXrmEasy.365     //For Dynamics 365 

//  Install-Package FakeXrmEasy.2016    //For Dynamics CRM 2016 

//  Install-Package FakeXrmEasy.2015    //For Dynamics CRM 2015 

//  Install-Package FakeXrmEasy.2013    //For Dynamics CRM 2013 

//  Install-Package FakeXrmEasy         //For Dynamics CRM 2011 

2. Your First Test 

            Now since we have both our plugin and test environment ready now let’s create a test project by using both early bound and Late Bound approach. 

a) Early Bound approach 

In D365 for Customer Engagement apps, the code generation tool creates early-bound entity classes that you can use to access business data in D365 for Customer Engagement. The classes include one class for each entity in your installation, including custom entities. Each time you make customizations to your system, you must regenerate these classes. Use the classes in any project type or build into a class library. You can use early-bound entity classes when creating applications that use Dynamics 365 for Customer Engagement as well as plug-ins and custom workflow activities.  

The benefit of using early-bound entity classes is that all type references are checked at compile time. Likewise, the compiled executable contains the code necessary to invoke the types’ properties, methods, and events. The sample of the Test Project using early bound is as follows: 

b) Late Bound approach 

            Late Bound is the traditional way of D365 coding where we generate proxy type assemblies by directly parsing the connection string, in this case no codes are generated.The sample of using Late bound for Unit test is as follows: 

With FakeXRMEasy, we are now able to write unit tests for D365 plugin’s and code activity. No more registration of plugin is required to test it but instead, you can create a mockup of your organization and proceed writing unit test for the same. 

Keep up to date with the latest technology by reading our other blogs. At Dogma Group, we help you uncover the right technology and implement it effectively. Contact us today for a no-obligation consultation on 01296 328 689. Or email us at info@dogmagroup.co.uk