unittesting

Plugin Registration tool’s profiling functionality has made plugin debugging a lot easier. The ease of use can sometimes tempt you to use this feature in the early stages of plugin development, to analyse what is state of PluginExecutionContext, InputParameter, Pre/Post Images etc. This gives you an understanding of the plugin’s flow and state, but only after it has been deployed to CRM. This, however, is not unit testing.

Right click on Microsoft.Xrm.Sdk in your unit test project and click on Add Fakes Assembly

Right click on System in your unit test project and click on Add Fakes Assembly

After you have added all your references you project should look something like this

Okay. Now what?

Now you can start writing your tests. To write an effective unit test you should start stubbing some classes in the Microsoft.Xrm.Sdk and overriding their behaviours. You have to use the following stubs inplace of their originals:

StubIPluginExecutionContext

StubITracingService

StubIOrganizationService

StubIServiceProvider

Scenario

You are developing a plugin which prevents the user from creating more than one opportunity for a customer.