Trying to debug a plugin in CRM 2011 can be extremely difficult. Not only are there problems with having .pdb files in the right place on the server, but every time you make changes to the encoding, you may encounter difficulties in deploying and re-registering the plugin. Since the trigger is in the CRM itself, it is difficult to create a unit test for it.
My current process of writing unit test for a completely new plugin is rather slow and error, but it happens like this:
- Register a new plugin using the SDK plugin registration tool
- Attach the debugger to w3wp.exe by placing a breakpoint in the plugin code.
- Run the plugin through any action that it registered to run.
- When the break point hits, serialize the inverse images, postimage, and pipeline target values โโin the XML files, then this will be the input to my unit test.
- Stop debugging and create a new unit test using RhinoMocks to mock the PluginExecutionContext and ServiceProvider, using loading serialized XML files as stubs for input parameters.
- Create methods that run at the beginning and at the end of each unit test, which resets (first tries to delete and then add) the dummy data for the unit test to process, and then deletes the dummy data at the end of the test.
- Edit Serialized files to refer to dummy data so that I can guarantee that the plugin will work against the same data every time it starts.
- Declare and instantiate the plugin in unit test, passing to the targeted objects
- Run the plugin by running additional queries to make sure that the plugin did the work that I expected, asserting on error.
It is a pain. From correctly capturing images, creating dummy data, and dumping it every time the test is performed, there seem to be many areas for improvement.
How can I unit test the plugin without actually launching it from CRM or running all hoopla debugging it in CRM and creating unique dummy data for each test? How can I use injection to eliminate the need to delete, create, test, verify and delete data in CRM for each unit test?
Update 2016
This question still raises quite a few hits, so I thought I'd add two (which I know) open source projects that provide Fake CRM instances for unit testing:
- FakeXrmEasy - Created by Jordi (see answer below)
- XrmUnitTest - Created by me
- Fake CRM + service more (assumptions, object collectors, etc.)
- Free plugin / workflow support
- No addiction to any mocking structure
- Sucky documentation (Im working on it)
Checkout this video I created to compare and compare differences.
c # unit-testing dynamics-crm-2011
Daryl
source share