2

我愿意开始为我的 CRM 2011 代码编写单元测试。我对单元测试的经验很少,所以我需要一些帮助。

我想测试的方法是:

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;

}

你对我可以写什么样的测试有一些想法吗?我应该模拟一些组件吗?

4

1 回答 1

2

您的方法只做两件事,更新 crmServiceObject 中的一些值,并执行更新语句。由于没有特定于 CRM 的内容(除了 crmServiceObject 是由 CRM 定义的类型之外)来更新对象的单元测试,我将跳过它的那部分。

至于禁用实体的 CRM 调用,您有两个用于单元测试的选项。验证 CRM 是否执行了禁用,或模拟 IOrganizationService 并断言是否进行了 Execute 调用。这真的取决于你。

这个问题也可能会有所帮助,尽管它专门与插件有关。

于 2013-11-05T16:03:50.503 回答