C# 如何使用FtpWebResponse创建WebException进行测试?

C# 如何使用FtpWebResponse创建WebException进行测试?,c#,unit-testing,exception-handling,ftpwebresponse,C#,Unit Testing,Exception Handling,Ftpwebresponse,我正在使用FtpWebRequest和一个瞬态故障处理应用程序块。对于我的故障处理程序,我有一个错误检测策略,检查响应是否被认为是暂时的,以便它知道是否重试: public bool IsTransient(Exception ex) { var isTransient = false; //will be false if the exception is not a web exception. var webEx = ex as W

我正在使用FtpWebRequest和一个瞬态故障处理应用程序块。对于我的故障处理程序,我有一个错误检测策略,检查响应是否被认为是暂时的,以便它知道是否重试:

public bool IsTransient(Exception ex)
    {

        var isTransient = false;
        //will be false if the exception is not a web exception.
        var webEx = ex as WebException;

        //happens when receiving a protocol error.
        //This protocol error wraps the inner exception, e.g. a 401 access denied.
        if (webEx != null && webEx.Status == WebExceptionStatus.ProtocolError)
        {
            var response = webEx.Response as FtpWebResponse;
            if (response != null && (int)response.StatusCode < 400)
            {
                isTransient = true;
            }
        }
        // if it is a web exception but not a protocol error,
        // check the status code.
        else if (webEx != null)
        {
            //(check for transient error statuses here...)
            isTransient = true;
        }

        return isTransient;
    }

有人知道我怎么做吗?我这样做对吗?

WebException
上使用适当的构造函数来设置响应:

public WebException(
 string message,
 Exception innerException,
 WebExceptionStatus status,
 WebResponse response)

用FtpWebResponse设置异常是我遇到的问题。。。FtpWebResponse有一个我无法访问的内部构造函数


BCL并不是真正为测试而设计的,因为这个概念在编写时并不重要。您必须使用反射调用该内部构造函数(使用反编译器查看可用内容)。或者,用自定义可模拟类包装所有需要的System.Net类。不过,这看起来需要做很多工作。

我使用Rhino Framework创建的FtpWebResponse存根构建离线测试

例如:

public WebException createExceptionHelper(String message, WebExceptionStatus webExceptionStatus, FtpStatusCode serverError )
{
    var ftpWebResponse = Rhino.Mocks.MockRepository.GenerateStub<FtpWebResponse>();
    ftpWebResponse.Stub(f => f.StatusCode).Return(serverError);
    ftpWebResponse.Stub(f => f.ResponseUri).Return(new Uri("http://mock.localhost"));

    //now just pass the ftpWebResponse stub object to the constructor
    return new WebException(message, null, webExceptionStatus, ftpWebResponse);
    }
公共WebException createExceptionHelper(字符串消息、WebExceptionStatus WebExceptionStatus、FtpStatusCode服务器错误)
{
var ftpWebResponse=Rhino.Mocks.MockRepository.GenerateSub();
ftpWebResponse.Stub(f=>f.StatusCode).Return(serverError);
ftpWebResponse.Stub(f=>f.ResponseUri).Return(新Uri(“http://mock.localhost"));
//现在只需将ftpWebResponse存根对象传递给构造函数
返回新的WebException(消息,null,webExceptionStatus,ftpWebResponse);
}

感谢您的回复。这就是我想要使用的,但是用FtpWebResponse设置一个异常是我遇到的麻烦。。。FtpWebResponse有一个我无法访问的内部构造函数。明白。BCL并不是真正为测试而设计的,因为这个概念在编写时并不重要。您必须使用反射调用该内部ctor(使用反编译器查看可用内容)。或者,用自定义可模拟类包装所有需要的System.Net类。不过,这看起来需要做很多工作。您好@usr,请将您的评论内容添加到您的答案中好吗?由于时间限制,我最终没有测试这个,但我相信你在这里的评论是一个更有用的答案。
public WebException createExceptionHelper(String message, WebExceptionStatus webExceptionStatus, FtpStatusCode serverError )
{
    var ftpWebResponse = Rhino.Mocks.MockRepository.GenerateStub<FtpWebResponse>();
    ftpWebResponse.Stub(f => f.StatusCode).Return(serverError);
    ftpWebResponse.Stub(f => f.ResponseUri).Return(new Uri("http://mock.localhost"));

    //now just pass the ftpWebResponse stub object to the constructor
    return new WebException(message, null, webExceptionStatus, ftpWebResponse);
    }