Trying to debug a plugin in CRM 2011 can be extremely difficult. Not only are there issues with having the .pdb files in the correct location on the server, but each time you make a coding change you get to go through the hassle of deploying and re-registering the plugin. Since the trigger is in CRM itself, it's hard to create a unit test for it.
My current process of writing a unit test for a brand new plugin is rather slow and error, but goes something like this:
- Register the new plugin using the SDK plugin registration tool
- Attach a debugger to the w3wp.exe, putting a break point in the plugin code.
- Trigger the plugin through whatever action it is registered to run for.
- When the break point gets hit, serialize the preimage, postimage, and target values of the pipeline to XML files, this then becomes the input to my unit test.
- Stop debugging and create a new unit test, using RhinoMocks to mock the PluginExecutionContext and ServiceProvider, using loading the serialized XML files as stubs for the input parameters.
- Create methods that get run at the start and end of each unit test that resets (first attempting to delete, then add) dummy data for the unit test to process, then deletes the dummy data at the end of the test.
- Edit the Serialized files to reference the dummy data so that I can ensure that the plugin will work against the exact same data each time it is ran.
- Declare and instantiate the plugin in the unit test, passing in the mocked objects
- Execute the plugin, running additional queries to ensure that the plugin performed the work I was expecting, Asserting on failure.
This is a pain to do. From getting the images correct, to creating the dummy data, and resetting it each time the test is run, there seems to be a lot of area for improvement.
How can I unit test a plugin without having to actually trigger it from CRM, or run through all the hoopla of debugging it in CRM first, and creating unique dummy data for each test? How can I use injection to eliminate the need to be deleting, creating, testing, verifying, and deleting data in CRM for each unit test?
Update 2016
This question is still getting quite a few hits, so I thought I'd add what the two (that I know of) open source projects that provide Fake CRM instances for unit testing:
- FakeXrmEasy -- Created by Jordi (see answer below)
- Primarily Fake CRM Service
- Support for Plugin/Workflow Faking
- Dependency on FakeItEasy
- Great Documentation
- XrmUnitTest -- Created by myself
- Fake CRM Service + more (Assumptions, Entity Builders, etc)
- Fluent Support for Plugin/Workflow Faking
- No Dependency on any mocking framework
- Sucky Documentation (I’m working on it)
Checkout this video I created to compare and contrast the differences.