【问题标题】:Mocking complex 3rd party libraries模拟复杂的第 3 方库
【发布时间】:2012-09-19 13:55:02
【问题描述】:

我有一个使用第 3 方 FTP 库 http://ftps.codeplex.com/ 的类,我想模拟它,以便我可以只对那个类而不是 FTP 库进行单元测试。我已经做到了,但对我来说感觉很乱。详细来说,该类使用了 AlexPilotti.FTPS.Client.FTPSClient 类的这些方法:

public string Connect(string hostname, ESSLSupportMode sslSupportMode)
public ulong PutFile(
    string localFileName, string remoteFileName,
    FileTransferCallback transferCallback)

委托 AlexPilotti.FTPS.Client.FileTransferCallback 如下所示:

public delegate void FileTransferCallback(
    FTPSClient sender, ETransferActions action, 
    string localObjectName, string remoteObjectName,
    ulong fileTransmittedBytes, ulong? fileTransferSize, ref bool cancel);

您会看到一个问题,因为 PutFile 从 FTP 库中获取了委托,并且该委托也从库中获取了两种不同的类型。我决定使用泛型与这些类型解耦。

为了能够创建模拟对象,我创建了一个接口,然后创建了一个派生类,它是 FTP 库功能的包装器。

// Interface so that dependency injection can be used.
public delegate void FileTransferCallback<T1, T2>(T1 sender, T2 action,
    string localObjectName, string remoteObjectName,
    ulong fileTransmittedBytes, ulong? fileTransferSize,
    ref bool cancel);

public interface IFTPClient<T1, T2> : IDisposable
{
    string Connect(string hostname, System.Net.NetworkCredential credential);
    ulong PutFile(
        string localFileName, string remoteFileName,
        FileTransferCallback<T1, T2> transferCallback);
}

// Derived class, the wrapper
using FTPSClient = FTPS.Client.FTPSClient;
using ETransferActions = FTPS.Client.ETransferActions;

public class FTPClient : IFTPClient<FTPSClient, ETransferActions>
{
    public FTPClient()
    {
        client = new AlexPilotti.FTPS.Client.FTPSClient();
    }

    public ulong PutFile(
        string localFileName, string remoteFileName,
        FileTransferCallback<FTPSClient, ETransferActions> transferCallback)
    {
        callback = transferCallback;
        return client.PutFile(localFileName, remoteFileName, TransferCallback);
    }

    public void Dispose()
    {
        client.Dispose();
    }

    public string Connect(
        string hostname, System.Net.NetworkCredential credential)
    {
        return client.Connect(hostname, credential, 
            FTPS.Client.ESSLSupportMode.ClearText);
    }

    void TransferCallback(
        FTPS.Client.FTPSClient sender, FTPS.Client.ETransferActions action, 
        string localObjectName, string remoteObjectName,
        ulong fileTransmittedBytes, ulong? fileTransferSize, 
        ref bool cancel)
    {
        callback.Invoke(sender, action, localObjectName, remoteObjectName,
            fileTransmittedBytes, fileTransferSize, ref cancel);
    }

    private AlexPilotti.FTPS.Client.FTPSClient client;
    private FileTransferCallback<FTPSClient, ETransferActions> callback;
}

这里有一个使用这个接口的类,我对它进行了单元测试。我把它删减了一点,所以只使用了 Connect 方法,但我希望它仍然能说明我的问题。

public class FTPServerConnection<T1, T2>
{
    public void Init(
        IFTPClient<T1, T2> client, string serverName,
        string userName, string passwd)
    {
        IsConnected = false;

        ServerName = serverName;
        UserName = userName;
        Passwd = passwd;

        ftpClient = client;

        Connect();
    }

    public void Connect()
    {
        ftpClient.Connect(
           ServerName,
           new System.Net.NetworkCredential(UserName, Passwd));
    }

    public string ServerName { protected set; get; }
    public string UserName { protected set; get; }
    public string Passwd { protected set; get; }

    public bool IsConnected { protected set; get; }

    private IFTPClient<T1, T2> ftpClient;
}

最后是单元测试:

[TestMethod()]
public void FTPServerConnectionConstructorTest()
{
    var ftpClient = new Mock<IFTPClient<object, object>>();
    var ftpServer = new FTPServerConnection<object, object>();
    ftpServer.Init(ftpClient.Object, "1.2.3.4", "user", "passwd");

    Assert.AreEqual("1.2.3.4", ftpServer.ServerName);
    Assert.AreEqual("user", ftpServer.UserName);
    Assert.AreEqual("passwd", ftpServer.Passwd);
}

在这种情况下,通常的方法是什么?我的方法似乎很混乱,我想知道是否有更简单的方法。谢谢。

【问题讨论】:

    标签: c# unit-testing moq mstest


    【解决方案1】:

    我认为这只是因为您要处理第三方委托而感到混乱,但对我来说这看起来像是一种非常标准的抽象/模拟方法。

    我唯一的评论是FTPServerConnection 的结构看起来有点不标准;按照它的编写方式,您必须实例化一个处于无效状态的实例(一个没有客户端详细信息的实例),然后调用 Init(),它本身调用 Connect()(从您的测试的外观来看,这绝不是由服务器类的客户端调用)。我会说在FTPServerConnection 构造函数中提供Init() 的参数会更正常,然后完全删除Init() 方法并让客户端执行此操作:

    var ftpClient = new Mock<IFTPClient<object, object>>();
    
    using (var ftpServer = new FTPServerConnection<object, object>(
         ftpClient.Object,
         "1.2.3.4",
         "user",
         "passwd"))
    {
        ftpServer.Connect();
    }
    

    ...但是,关于您抽象 FTP 库的方式,我认为这是一种不错的方式:)

    【讨论】:

    • 我担心没有其他方法可以做到这一点;)感谢 Init() 方法,我会改变它。
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 2018-05-03
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    相关资源
    最近更新 更多