Unit Testing Technology for DynamicsCRM programming: MicrosoftFakeFramework
For programming based on complex frameworks, the most difficult part is not writing code, but how to quickly and effectively debug wrong code and how to efficiently and accurately complete code unit tests. Taking the compilation of Dynamics CRM Plugin, it is not difficult to compile a plug-in with thousands of lines of code. However, what if we debug thousands of lines of code and make it effective and accurate for our purposes? This is also the core gap between junior technicians and senior technicians.
Today I will bring you an article on plug-in unit testing. The blogger wrote another similar unit testing document two years ago, using the Rhino Mock technology. However, the technology we want to use today is Microsoft Fake Framework technology.
What is Fake Framework? It is a powerful Unit Test Framework launched by Microsoft. It has fundamental differences and advantages with Tools based on OOP concepts such as Rhino Mock. It can test private methods and rewrite the closed code of the system framework. Of course, its functions are far from the end of this process. If you are interested, you can gain an in-depth understanding of it.
To debug the unit of Dynamics CRM Plugin, you must solve the problem. Of course, you must simulate and construct IServiceProvider and its parameters. You don't have to worry too much about this knowledge. You can refer to the Demo given below.
Public IOrganizationService crmService; public IOrganizationServiceFactory crmServiceFactory; public IPluginExecutionContext context; public ITracingService tracing; public IServiceProvider serviceProvider; public Guid UserId = Guid. empty; private const string CRM_SVC_PATH = "https: // your server/XRMServices/2011/Organization. svc "; private const string CRM_USERNAME =" account "; private const string CRM_PASSWORD =" pwd "; [TestInitialize] public void Startup () {crmService = CRMHelperV6.GetOrganizationServiceProxy (new Uri (CRM_SVC_PATH ), authenticationProviderType. federation, new string [] {CRM_USERNAME, CRM_PASSWORD}); // construct the CRM Proxy crmServiceFactory = new Microsoft. xrm. sdk. fakes. stubIOrganizationServiceFactory () {CreateOrganizationServiceNullableOfGuid = (id) =>{ return crmService ;}}; serviceProvider = new System. fakes. stubIServiceProvider () {GetServiceType = (a) =>{ object result = new object (); if (a = typeof (IOrganizationServiceFactory) {result = crmServiceFactory ;} else if (a = typeof (IPluginExecutionContext) {result = context;} if (a = typeof (ITracingService) {} return result ;}};} [TestMethod] public void CreateInvoiceDetailToXero () {Entity invoiceDetail = new Entity ("invoicedetail"); invoiceDetail. attributes. add ("invoiceid", new EntityReference ("invoice", Guid. parse ("{C1CCE633-D555-E511-8104-C4346BADD6F4}"); invoiceDetail. attributes. add ("quantity", 2.0 m); invoiceDetail. attributes. add ("productid", new EntityReference ("product", Guid. parse ("{6739bcb6-6eb4-e411-80df-c4425bade648}"); invoiceDetail. attributes. add ("baseamount", new Money (1000); invoiceDetail. attributes. add ("invoicedetailid", Guid. parse ("{D4E44A0F-E255-E511-8104-C4346BADD6F4}"); invoiceDetail. attributes. add ("q2_synctoxero", true); ParameterCollection inputParameter = new ParameterCollection (); inputParameter. add ("Target", invoiceDetail); context = new Microsoft. xrm. sdk. fakes. stubIPluginExecutionContext () {InputParametersGet = () =>{ return inputParameter ;}}; CrmIntegrationPlugins. invoiceDetailPlugin invD = new InvoiceDetailPlugin (); invD. execute (serviceProvider );}
Here, we first construct a real CRM Proxy and simulate IServiceProvider and its parameters. Of course, you can also simulate Target, PreImage, and PostImage based on your specific needs, simulate input parameters and perform Mock Processing Based on specific services.