Testing a Dynamics CRM workflow with Rhino Framework

227 views Asked by At

Can anyone provide me with code sample for unit testing a workflow with Rhino Framework. More specificly I cannot seem to stub the CodeActivityContext object which is a parameter for workflow.execute method. I am very new to the Rhino and that does not help either.

1

There are 1 answers

0
BlueSam On

I recommend a different approach to unit testing, which differs from most blogs and posts.

I recommend getting the IOrganizationService and any data or identifiers that you need in order to execute your function. I then recommend putting the function that does the heavy lifting (logic, CRUD operations, etc.) in a separate class. This way, you only have to create a mock of the IOrganizationService. Here's an example:

public class AccountContactManager
{
    private IOrganizationService _service;
    public AccountContactManager(IOrganizationService service)
    {
        _service = service;
    }

    public void UpdateAccountContacts(Guid accountId)
    {
        this._service.Execute();//do something here with the service
    }
}

And here's an example of how to get what you need out of the CodeActivityContext:

var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var recordId = context.PrimaryEntityId;
var preImage = context.PreEntityImages["PreImage"];
var postImage = context.PostEntityImages["PostImage"];
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var organizationService = result.Factory.CreateOrganizationService(context.UserId);
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var userId = context.UserId;
var record = context.InputParameters["Target"];
if (record is Entity)
{
    var entityRecord = (Entity)record;
}
else
{
    var recordReference = (EntityReference)record;
}