I’m going to test same plugin as I tested in previous post. Plugin creates a task to qualify lead in two weeks each time lead is created. I use MS Test as unit testing framework.
Structure of the solution is explained in previous post. It much easy to get the thing right if you take a look on part 1: "Unit testing CRM 2011 plugins. Approaches - Part 1".
The main idea here is to mock objects (pipeline context, target and output properties, Organisation Service) we need using one of mock libraries. In examples below Rhino Mocks is used.
The main advantage of this approach is opportunity to run tests and develop/debug plugins without crm being installed. Such tests can be easily included in TFS build process.
You can combine this approach with any approach from previous post.
Read following for more details: http://www.develop1.net/public/post/Unit-Testing-Dynamics-CRM2011-Pipeline-Plugins-using-Rhino-Mocks.aspx
Using Mocks + Business Logic Testing
Using Mocks + Plugin testing
Using Serialized context
Instead of mocking objects we can use previously serialized objects. This approach allows as to develop/debug plugins. Unfortunately you can’t check results after plugin execution. So there is no way to create tests using this approach.
You can read more here: http://exploringxrm.wordpress.com/2012/01/23/unit-testing-crm-2011-plugin
In following example saved context is read from leadOneCreateContext.xml and passed to the plugin’s Execute method:
Happy testing :)
You can download Visual Studio project here: