How to configure C # unit tests for CRM 2011 plugins?

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.

+8
c # unit-testing dynamics-crm-2011
source share
3 answers

How can I test a unit test to connect to a plugin without having to run it from CRM or skip all the debugging clips in CRM and create unique dummy data for each test?

With a mockery. See this link for what classes are mocked with RhinoMocks. You seem to be on your way in this regard.

How can I use injection to eliminate the need to delete, create, test, verify and delete data in CRM for each unit test?

Injection values โ€‹โ€‹for the input parameters can be performed by trimming in an instance with a manual drive of the object with which you are going to manipulate:

// Add the target entity Entity myStubbedEntity = new Entity("account"); // set properties on myStubbedEntity specific for this test... ParameterCollection inputParameters = new ParameterCollection(); inputParameters.Add("Target", myStubbedEntity); pipelineContext.Stub(x => x.InputParameters).Return(inputParameters); 

Isnโ€™t it easier than capturing xml data and rehydrating the entire collection of input parameters?


EDIT: For data access, the usual recommendation is to wrap data access in classes. The repository template is popular, but redundant for what we need here. For plugin runtime classes, you โ€œenterโ€ your mocked class when you create it. An empty constructor that initializes the default repository, and a second constructor that accepts an IRepository.

 public class MyPluginStep { ITaskRepository taskRepository; public MyPluginStep(ITaskRepository repo) { taskRepository = repo; } public MyPluginStep() { taskRepository = new DefaultTaskRepositoryImplementation(); } public MyExecuteMethod(mypluginstepparams){ Task task = taskRepository.GetTaskByContact(...); } 

Depending on the complexity of your plug-in steps, this may turn into passing many repositories to each class and may become burdensome, but it is the basis to which you can add complexity if necessary.

+2
source share

I am serializing the plugin execution context to a file for use with unit tests. There is a good project for codeplex that does this http://crm2011plugintest.codeplex.com/

Ease in debugging and unit testing, and you can "record" testing in the real world.

+4
source share

One really good option is to use a mocking library that deals with fraud and fakes for you because I wanted to create my own and always ended up wasting time creating fakes or mock-ups until I created this library that does it for you. Try FakeXrmEasy

+2
source share

All Articles