0

我面临以下问题,我正在尝试使用我需要的属性创建一个假的 ftp-response,但是由于 FtpWebResponse 没有构造函数,一切都崩溃了。我收到以下错误:

 AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from System.Net.FtpWebResponse because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.

这是我的部分代码不起作用:

     var fixture = new Fixture();
        //fixture.Customize<FtpWebResponse>(c => c.OmitAutoProperties());
        fixture.Customize<FtpWebResponse>(c => c.FromFactory(
            new MethodInvoker(
                new GreedyConstructorQuery()))
         .OmitAutoProperties());

        var response = fixture.Create<FtpWebResponse>();

        var field_0 = typeof(FtpWebResponse).GetField("<StatusCode>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
        field_0.SetValue(response, FtpStatusCode.CommandOK);
        var field_1 = typeof(FtpWebResponse).GetField("<ContentLength>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
        field_1.SetValue(response, responseStream.Length);
        var field_2 = typeof(FtpWebResponse).GetField("<ContentType>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
        field_2.SetValue(response, "bytes");

        var field_3 = typeof(FtpWebResponse).GetMethod("GetStream");
        field_3.Invoke(responseStream, null);

        var client = Substitute.For<IFtpClient>();
        client.GetAsync(new Uri(_url)).ReturnsForAnyArgs(response);

任何意见,将不胜感激 :)

4

0 回答 0