单元测试 FTPWebRequest/FTPWebResponse

发布于 2024-10-28 04:47:07 字数 58 浏览 1 评论 0原文

您将如何通过最小起订量对 FTPWebRequest 和 FTPWebResponse 进行单元测试。

How would you unit test FTPWebRequest and FTPWebResponse through MOQ.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄意 2024-11-04 04:47:07

您不能模拟 FTPWebRequestFTPWebResponse 带有 Moq,因为它只允许您模拟接口或抽象类。而且看起来微软在编写大部分 System.Net 命名空间时并没有考虑可测试性。这是我从 Moq 转向 RhinoMocks 的主要原因。

您需要构建自己的 FTPWeb* 对象并将它们传递给您的处理程序。

You can't mock FTPWebRequest or FTPWebResponse with Moq, because it only allows you to mock interfaces or abstract classes. And it doesn't look like MS was thinking about testability when they wrote most of the System.Net namespace. That's the primary reason I've moved away from Moq to RhinoMocks.

You'll need to build your own FTPWeb* objects and pass them to your handler.

淡笑忘祈一世凡恋 2024-11-04 04:47:07

对于 Mock 也是不可能的,因为 FTPWebResponse 没有公开允许从中派生某些内容的构造函数。

这是我在类似情况下编写测试的方法。

测试方法:ExceptionContainsFileNotFound(Exception ex)
包含以下逻辑:

if (ex is WebException)
{
    var response = (ex as WebException).Response;
    if (response is FtpWebResponse)
    {
        if ((response as FtpWebResponse).StatusCode == FtpFileNotFoundStatus)
        {
            return true;
        }
    }
}

为了测试它,我实现了快速技巧。

try
{
    var request = WebRequest.Create("ftp://notexistingfptsite/");
    request.Method = WebRequestMethods.Ftp.ListDirectory;

    request.GetResponse();
}
catch (WebException e)
{
    // trick :)
    classUnderTest.FtpFileNotFoundStatus = FtpStatusCode.Undefined;

    var fileNotFoundStatus = classUnderTest.ExceptionContainsFileNotFound(e);

    Assert.That(fileNotFoundStatus, Is.True);
}

(当然,FtpFileNotFoundStatus 不会暴露给世界。)

Not possible with Mock also because FTPWebResponse doesn't have constructors exposed to allow something to be derived from it.

Here is how I wrote my test in similar situation.

Method under test: ExceptionContainsFileNotFound(Exception ex)
contains following logic:

if (ex is WebException)
{
    var response = (ex as WebException).Response;
    if (response is FtpWebResponse)
    {
        if ((response as FtpWebResponse).StatusCode == FtpFileNotFoundStatus)
        {
            return true;
        }
    }
}

In order to test it I implemented quick trick.

try
{
    var request = WebRequest.Create("ftp://notexistingfptsite/");
    request.Method = WebRequestMethods.Ftp.ListDirectory;

    request.GetResponse();
}
catch (WebException e)
{
    // trick :)
    classUnderTest.FtpFileNotFoundStatus = FtpStatusCode.Undefined;

    var fileNotFoundStatus = classUnderTest.ExceptionContainsFileNotFound(e);

    Assert.That(fileNotFoundStatus, Is.True);
}

(Of course FtpFileNotFoundStatus is not exposed to the World.)

明媚如初 2024-11-04 04:47:07

为此,我使用 Rhino 框架。

即使没有公共构造函数、只读属性等,它也可以处理实例创建。

例子:

var ftpWebResponse = Rhino.Mocks.MockRepository.GenerateStub<FtpWebResponse>();
ftpWebResponse.Stub(f=>f.StatusCode).Return(FtpStatusCode.AccountNeeded);

For that i use Rhino frameWork.

It can handle instance creation even if there is no public constructor, read only properties and more.

Example:

var ftpWebResponse = Rhino.Mocks.MockRepository.GenerateStub<FtpWebResponse>();
ftpWebResponse.Stub(f=>f.StatusCode).Return(FtpStatusCode.AccountNeeded);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文