title: How do I correctly setup C# unit tests for CRM 2011 plugins?
body: <p>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.</p>
  
  <p>My current process of writing a unit test for a brand new plugin is rather slow and error, but goes something like this:</p>
  
  <ol>
  <li>Register the new plugin using the SDK plugin registration tool</li>
  <li>Attach a debugger to the w3wp.exe, putting a break point in the plugin code.</li>
  <li>Trigger the plugin through whatever action it is registered to run for.</li>
  <li>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.</li>
  <li>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.</li>
  <li>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.</li>
  <li>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. </li>
  <li>Declare and instantiate the plugin in the unit test, passing in the mocked objects</li>
  <li>Execute the plugin, running additional queries to ensure that the plugin performed the work I was expecting, Asserting on failure.</li>
  </ol>
  
  <p>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.  </p>
  
  <p>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?</p>
  
  <h2>Update 2016</h2>
  
  <p>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:</p>
  
  <ul>
  <li><a href="https://github.com/jordimontana82/fake-xrm-easy" rel="nofollow">FakeXrmEasy</a> -- Created by Jordi (see answer below)
  
  <ul>
  <li>Primarily Fake CRM Service</li>
  <li>Support for Plugin/Workflow Faking</li>
  <li>Dependency on FakeItEasy</li>
  <li><a href="http://dynamicsvalue.com/get-started/overview" rel="nofollow">Great Documentation</a> </li>
  </ul></li>
  <li><a href="https://github.com/daryllabar/XrmUnitTest" rel="nofollow">XrmUnitTest</a> -- Created by myself
  
  <ul>
  <li>Fake CRM Service + more (Assumptions, Entity Builders, etc)</li>
  <li>Fluent Support for Plugin/Workflow Faking</li>
  <li>No Dependency on any mocking framework</li>
  <li>Sucky Documentation (I’m working on it)</li>
  </ul></li>
  </ul>
  
  <p>Checkout <a href="https://youtu.be/dRz1X6pdsk4" rel="nofollow">this video</a> I created to compare and contrast the differences.</p>