【问题标题】:How to unit test Soap service access from Crm 2011 Silverlight application?如何对来自 Crm 2011 Silverlight 应用程序的 Soap 服务访问进行单元测试?
【发布时间】:2013-10-21 14:01:12
【问题描述】:

在 Dynamics CRM 2011 的 Silverlight 5 应用程序中,我访问 CRM 的组织服务以查询实体元数据。我编写了一个服务,它接受一个实体名称并返回其所有字段的列表。

如何自动测试此服务方法?主要问题是如何从不在 CRM 上下文中运行的 Silverlight 应用程序获取对组织服务的引用。

我的服务方法如下所示:

public IOrganizationService OrganizationService
    {
        get
        {
            if (_organizationService == null)
               _organizationService = SilverlightUtility.GetSoapService();
            return _organizationService;
        }
        set { _organizationService = value; }
    }


public async Task<List<string>> GetAttributeNamesOfEntity(string entityName)
    {
        // build request
        OrganizationRequest request = new OrganizationRequest
            {
                RequestName = "RetrieveEntity",
                Parameters = new ParameterCollection
                    {
                        new XrmSoap.KeyValuePair<string, object>()
                            {
                                Key = "EntityFilters",
                                Value = EntityFilters.Attributes
                            },
                        new XrmSoap.KeyValuePair<string, object>()
                            {
                                Key = "RetrieveAsIfPublished",
                                Value = true
                            },
                        new XrmSoap.KeyValuePair<string, object>()
                            {
                                Key = "LogicalName",
                                Value = "avobase_tradeorder"
                            },
                        new XrmSoap.KeyValuePair<string, object>()
                            {
                                Key = "MetadataId",
                                Value = new Guid("00000000-0000-0000-0000-000000000000")
                            }
                    }
            };

        // fire request
        IAsyncResult result = OrganizationService.BeginExecute(request, null, OrganizationService);

        // wait for response
        TaskFactory<OrganizationResponse> tf = new TaskFactory<OrganizationResponse>();
        OrganizationResponse response = await tf.FromAsync(
            OrganizationService.BeginExecute(request, null, null), iar => OrganizationService.EndExecute(result));

        // parse response
        EntityMetadata entities = (EntityMetadata)response["EntityMetadata"];
        return entities.Attributes.Select(attr => attr.LogicalName).ToList();
    }

编辑: 我可以使用 Resharper 和 AgUnit 创建和执行单元测试。因此,问题通常不在于如何编写单元测试。

【问题讨论】:

  • 您好,Dynamics CRM 有一个单元测试框架,它可以做到这一点,并为您伪造大部分常见消息。该框架是完全免费的,也可以在 Git 上使用。请check the getting started page

标签: silverlight unit-testing dynamics-crm-2011 automated-tests dynamics-crm


【解决方案1】:

我已调整标准 Microsoft SDK 中的 GetSoapService 以接受回退值。这意味着在 Visual Studio 中调试和在 CRM 中运行时无需更改代码。反正就在这里

public static IOrganizationService GetSoapService(string FallbackValue = null)
    {
        Uri serviceUrl = new Uri(GetServerBaseUrl(FallbackValue)+ "/XRMServices/2011/Organization.svc/web");


        BasicHttpBinding binding = new BasicHttpBinding(Uri.UriSchemeHttps == serviceUrl.Scheme
            ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.TransportCredentialOnly);
        binding.MaxReceivedMessageSize = int.MaxValue;

        binding.MaxBufferSize = int.MaxValue;

        binding.SendTimeout = TimeSpan.FromMinutes(20);

        IOrganizationService ser =new OrganizationServiceClient(binding, new EndpointAddress(serviceUrl));

        return ser;


    }

public static string GetServerBaseUrl(string FallbackValue = null)
    {


        try
        {
            string serverUrl = (string)GetContext().Invoke("getClientUrl");
            //Remove the trailing forwards slash returned by CRM Online
            //So that it is always consistent with CRM On Premises
            if (serverUrl.EndsWith("/"))
            {
                serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
            }

            return serverUrl;
        }
        catch
        {
            //Try the old getServerUrl
            try
            {
                string serverUrl = (string)GetContext().Invoke("getServerUrl");
                //Remove the trailing forwards slash returned by CRM Online
                //So that it is always consistent with CRM On Premises
                if (serverUrl.EndsWith("/"))
                {
                    serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
                }

                return serverUrl;
            }
            catch
            {
                return FallbackValue;
            }
        }

【讨论】:

  • 感谢您的建议。如果我正确理解了您的方法,它将处理在测试场景和生产场景中提供 CRM 的 URL。我尝试了类似的方法,但我的方法导致身份验证失败。这是有道理的,因为在 CRM 上下文中,身份验证上下文已经存在。在我的测试环境中它不是。
  • 我取决于您使用的是基于声明的身份验证还是普通的 Windows 身份验证。 Windows 方式可以正常工作,但对于索赔,我认为您需要从索赔服务器获取“令牌”。我对此无能为力,因为我们的 CRM 不是这样设置的
  • 很高兴知道这一点。现在我知道在哪里看才能让它工作。感谢您的提示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 2011-06-22
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
相关资源
最近更新 更多