Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Silverlight 5单元测试异步方法_C#_Silverlight_Unit Testing_Asynchronous_Dynamics Crm 2011 - Fatal编程技术网

C# 如何从Silverlight 5单元测试异步方法

C# 如何从Silverlight 5单元测试异步方法,c#,silverlight,unit-testing,asynchronous,dynamics-crm-2011,C#,Silverlight,Unit Testing,Asynchronous,Dynamics Crm 2011,编辑:上下文:我编写了一个Silverlight应用程序,通过Soap服务访问Dynamics CRM 2011。为此,我实现了一个服务。我现在想为这个服务编写一个单元测试 我想为以下方法编写一个单元测试: public async Task<List<string>> GetAttributeNamesOfEntity(string entityName) { // build request OrganizationReques

编辑:上下文:我编写了一个Silverlight应用程序,通过Soap服务访问Dynamics CRM 2011。为此,我实现了一个服务。我现在想为这个服务编写一个单元测试

我想为以下方法编写一个单元测试:

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(result, iar => OrganizationService.EndExecute(result));

        // parse response
        EntityMetadata entities = (EntityMetadata)response["EntityMetadata"];
        return entities.Attributes.Select(attr => attr.LogicalName).ToList();
    }
我的第一个方法是调用实际的CRM。此操作失败,因为我无法进行身份验证。我问了一个问题。我的第二种方法是模拟组织服务,并在模拟的基础上运行该方法。像这样:

    private bool _callbackCalled = false;
    private SilverlightDataService _service;
    private List<string> names;

    [TestInitialize]
    public void SetUp()
    {
        IOrganizationService organizationService = A.Fake<IOrganizationService>();
        _service = new SilverlightDataService {OrganizationService = organizationService};

        IAsyncResult result = A.Fake<IAsyncResult>();
        A.CallTo(organizationService.BeginExecute(A<OrganizationRequest>.Ignored, A<AsyncCallback>.Ignored,
                                                  A<object>.Ignored)).WithReturnType<IAsyncResult>().Returns(result);

        EntityMetadata entities = new EntityMetadata();
        AttributeMetadata meta = new AttributeMetadata();
        meta.LogicalName = "foobar";
        entities.Attributes = new ObservableCollection<AttributeMetadata>();
        entities.Attributes.Add(meta);
        OrganizationResponse response = new OrganizationResponse();
        response.Results = new ParameterCollection();
        response["EntityMetadata"] = entities;


        A.CallTo(() => result.IsCompleted).Returns(true);
        A.CallTo(result.AsyncState).WithReturnType<object>().Returns(response);
        A.CallTo(organizationService.EndExecute(result)).WithReturnType<OrganizationResponse>().Returns(response);
    }


    [TestMethod]
    [Asynchronous]
    public async void TestGetAttributeNamesOfEntity()
    {
        TaskFactory<List<string>> tf = new TaskFactory<List<string>>();
        names = await tf.FromAsync(_service.GetAttributeNamesOfEntity("avobase_tradeorder"), CallbackFunction);

        Assert.IsTrue(_callbackCalled);
        Assert.IsNotNull(names);
    }

    /// <summary>
    /// is used as callback for the method GetAttributeNamesOfEntity
    /// </summary>
    /// <param name="result"></param>
    /// <returns></returns>
    private List<string> CallbackFunction(IAsyncResult result)
    {
        _callbackCalled = true;
        names = (List<string>) result.AsyncState;
        return null;
    }
我用假笑话来嘲弄。当我使用ReSharper和AgUnit执行此测试时,它将永远等待。我也试过了,但没用

我的假设是我的模拟不会告诉等待魔法任务已经完成。为什么等待永远不会结束?我要怎么做才能解决这个问题

2是否有更好的方法来实现这一点


编辑:我使用的是:VS 2012 Premium、ReSharper 7.1、Silverlight 5、AgUnit 0.7、.Net 4.5、Fakeitesy 1.13.1,通过NuGet安装。

我认为测试方法应该是异步任务,而不是异步无效。还有,你读过Skeet's吗?试试看,这是一个在Fakeitasy之上的图层,它为你添加了所有的嘲弄,你大部分时间都会花在哪里。git上的开源项目。