CRM Plug-in Unit Testing Walkthrough

CRM Plug-in Unit Testing Walkthrough

Connect to CRM Online 2011

Connect to Dynamic CRM Online

1. VS will create the following solution. Remove Workflow project and a reference to it.

Solution Explorer image

Here is what should be obtained as a result:

Solution Explorer result image

2. Add a new plug-in and register it on the particular message:

CRM Plud-in Unit Testing Procedure

Follow the steps below:
1. Open CRM Explorer

Open CRM Explorer

2. Expand Entities tree-node, right click on Customer entity and select Create Plug-in.

Create Plug-in in Dynamic CRM

3. VS will display the following:

Plug-in in MS Dynamic CRM

Set Post Operation in Pipeline Stage, then select Update message and click OK. VS will create PostCustomerUpdate class and register a new step in CRM on Update message. Every time the Customer is updated PostCustomerUpdate plug-in will be triggered.

Post Operation in MS CRM

4. Add new folders DAL and BL to Plugins project. Create two files: CustomerDao.cs and CustomerBl.cs and place them into DAL and BL folders respectively.

Plugins in MS CRM

Make CustomerBl and CustomerDao classes public. Add the interface named ICustomerDao to DAL folder and implement it into CustomerDao.

5. Check whether references to Microsoft.Xrm.Sdk and Microsoft.Crm.Sdk.Proxy exist. If not, add them from the SDK\bin folder.

6. Make changes in ExecutePostCustomerUpdate method to get Customer entity, create an instance of CustomerBl and call method UpdateCustomer in PostCustomerUpdate as it follows:

// note: your custom Plug-in business logic.
Contact customer;
if (localContext.PluginExecutionContext.InputParameters != null &&
localContext.PluginExecutionContext.InputParameters.Contains("Target") &&
localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
{
var entity = В В В (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
if (!entity.LogicalName.Equals(Contact.EntityLogicalName))
throw new ArgumentException("Plugin execution failure. Can not retrieve required entity.");
customer = entity.ToEntity<Contact>();
}
else
{
throw new ArgumentException("Plugin execution failure. Can not retrieve required entity.");
}
var manager = new CustomerBl(new CustomerDao(), customer);
manager.OnUpdateCustomer();

7. Generate the wrapper object using SDK utility crmsvcutil /nologo /language:CS /url:https://developmentplatform.crm4.dynamics.com/XRMServices/2011/Organization.svc /out:wt_entities.cs /username:[email protected] /password:verystrongpassw0rd /serviceContextName:PluginOrgContext. Add this file to Plugin project.

8. Add property PluginOrgContext PluginContext to inner LocalPluginContext class and instantiate it in the constructor.

internal PluginOrgContext PluginContext { get; private set; }

…

this.PluginContext = new PluginOrgContext(OrganizationService);

9. Add code to CustomerBl.cs:

private ICustomerDao _dao;
private Contact _customer;
public CustomerBl(ICustomerDao dao, Contact contact)
{
_dao = dao;
_customer = contact;
}

10. Change method

public void OnUpdateCustomer()
{
throw new NotImplementedException();
}

To

public void OnUpdateCustomer()
{
throw new InvalidPluginExecutionException("Customer Updated!");
}

11. Sign Plug-in project with the key and rebuild the solution.

Plug-in project in Dynamic CRM

12. Publish newly-created plug-in to CRM Online:

Publish plug-in in Dynamic CRM

You can also use a Plug-in registration tool.

13. Go to CRM Screen, open Customer to edit, make changes and click вЂ?Save’ button on the Ribbon. The error message should appear:

Error message in CRM

It indicates that plug-in works correctly. Notice that each time the Customer is updated, plug-in is triggered. Therefore, remove a throwing exception, rebuild the solution and deploy it again. Run and check if everything works fine.

14. Now, we’re going to create the first test. Right click on Plug-ins project and select вЂ?Run Pex’

Run Pex in MS Dynamic CRM

Pex will start the exploration. The result of exploration should be as follows:

Pex in Dynamic CRM

You can see Pex Explorer view. Here you can view information about methods and generated tests, including passed/failed tests reports.

Pex Explorer in MS Dynamic CRM

Contact us

Tell your idea, request a quote or ask us a question