MaPi
MaPi

Reputation: 1601

How to write a Unit Test in CRM 2011 development

I am willing to start writing Unit Tests for my CRM 2011 code. I have very little experience of Unit Testing, so I would like some help with it.

The method I would like to test is:

public Proxy.custom_serviceobject PopulateServiceObject(Proxy.custom_serviceobject crmServiceObject, serviceobject irisServiceObject, Guid locationId)
{
    //set the reference to the location the order is connected to
    crmServiceObject.custom_location = new EntityReference("custom_location", locationId);
    //add the reference to the product in the serviceobject
    crmServiceObject.custom_product = new EntityReference("product", new Guid(irisServiceObject.ItemId));
    //convert the errorid to an int
    Int32 errorId;
    var success = Int32.TryParse(irisServiceObject.ErrorId, out errorId);
    crmServiceObject.custom_errorclassOptionSetValue = success ? new OptionSetValue(errorId) : new OptionSetValue(Int32.MinValue);

    crmServiceObject.custom_serviceobjecttype =
        new EntityReference("custom_serviceobjecttype", new Guid(irisServiceObject.ObjectType.Id));

    crmServiceObject.custom_placement = irisServiceObject.SortId;
    crmServiceObject.custom_yearofinstallationOptionSetValue = new OptionSetValue(irisServiceObject.AuditYear);
    crmServiceObject.custom_yearofmanufactureOptionSetValue = new OptionSetValue(irisServiceObject.ProductionYear);
    crmServiceObject.custom_location = new EntityReference("custom_location", new Guid(irisServiceObject.Location));
    crmServiceObject.custom_quantity = irisServiceObject.Qty;

    crmServiceObject.custom_remark = irisServiceObject.ExternalNote;
    crmServiceObject.custom_internalremark = irisServiceObject.InternalNote;



    if (irisServiceObject.Cancelled)
    {
        var setStateRequest = new SetStateRequest
        {
            EntityMoniker = new EntityReference
            {
                Id = crmServiceObject.Id,
                LogicalName = "custom_serviceobject"
            },
            State = new OptionSetValue(StatusInactive),
            Status = new OptionSetValue(StatusReasonInactive)
        };
        Service.Execute(setStateRequest);
    }

    return crmServiceObject;

}

Do you have some ideas about what kind of test I could write? Should I mock some components?

Upvotes: 2

Views: 524

Answers (1)

Daryl
Daryl

Reputation: 18895

Your method only does two things, update some values in the crmServiceObject, and execute an update statement. Since there is nothing CRM specific (besides besides the crmServiceObject being a type defined by CRM) to unit testing updating an object I'll skip that part of it.

As far as the CRM call to disable the entity, you've got two options for a unit test. Verify that CRM performed a disable or not, or mock an IOrganizationService and assert that the Execute call was made or not. It's up to you really.

This question may be helpful as well, although it relates to plugins specifically.

Upvotes: 2

Related Questions