Showing posts with label Testing. Show all posts
Showing posts with label Testing. Show all posts

Sunday, February 13, 2011

CRM 2011 plug-in tips and tricks (part 2) - Testing plug-ins

Testing CRM plug-ins is now much easier than it was in previous version of CRM.   I will often create a simple console application that executes the plug-in I'm developing, just so I can make sure the core functionality of the plug-in works before I ever have to deploy it to the CRM server.  The same techniques can also be used for writing unit tests for your plug-ins.  

To create a simple test harness for your plug-ins you just need to create a few classes that implement the IServiceProvider, IOranizationServiceFactory, and IPluginExecutionContext interfaces. 


First is the TestServiceProvider Class.  This class is responsible for providing the plugin execution context and service factory to the plug-in.

public class TestServiceProvider : IServiceProvider
{
    private TestPluginContext _pluginContext;
    public TestServiceProvider(TestPluginContext pluginContext)
    {
        _pluginContext = pluginContext;
    }

    public object GetService(Type serviceType)
    {
        if (serviceType == typeof(IPluginExecutionContext))
        {
            return _pluginContext;
        }
        if (serviceType == typeof(IOrganizationServiceFactory))
        {
            return new TestServiceFactory();
        }
        return null;
    }
}