Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:06:49+00:00 2026-06-11T17:06:49+00:00

I have a class that uses 3rd party FTP library http://ftps.codeplex.com/ and I would

  • 0

I have a class that uses 3rd party FTP library http://ftps.codeplex.com/ and I would like to mock it so that I can unit test just that class and not the FTP library. I have done it but it feels messy to me. In detail, the class uses these methods of AlexPilotti.FTPS.Client.FTPSClient class:

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

Delegate AlexPilotti.FTPS.Client.FileTransferCallback looks like this:

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

You can see a problem because PutFile takes the delegate from FTP library and that delegate takes two different types from the library as well. I decided to use generics to decouple from these types.

To be able to create a mock object, I created an interface and then a derived class which is a wrapper of the FTP library functionality.

// 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;
}

Here goes a class that uses this interface and which I unit test. I stripped it down a bit so only the Connect method is used but I hope it still illustrates my problem.

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;
}

And finally the unit test:

[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);
}

What is the usual approach in situations like this one? My approach seems to be messy and I am wondering if there is easier way of doing it. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:06:50+00:00Added an answer on June 11, 2026 at 5:06 pm

    I think this only feels messy because you’ve had that third party delegate to cope with, but it looks like a pretty standard abstraction / mocking approach to me.

    My only comment would be that the structure of FTPServerConnection looks a bit non-standard; the way it’s written you have to instantiate one in an invalid state (i.e. one with no client details) then call Init(), which itself calls Connect() (which from the look of your test is never called by a client of the server class). It would be more normal I’d say to have the arguments to Init() supplied in the FTPServerConnection constructor, then remove the Init() method altogether and have the client do this:

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

    …but again, regarding the way you’ve abstracted the FTP library, I think that’s a decent way of doing it 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class that uses boost mutexes and locks like this (only
I have a Java class library (3rd party, proprietary) and I want my python
So I have a class that uses references (&) with functions like void request(tcp::socket&
I have a project that uses a few 3rd party DLLs, and am working
I have a class that unmarshals xml from a 3rd party source (I have
I have a class that uses My.Computer.Network.Ping to get a boolean value on whether
I have a class that uses XStream and is used as a transfer format
i have a class that uses a proxy class inside to call a service
I have a class that uses org.apache.ant and the class is very simple, it's
I have a class that uses priority queue to display 5 strings in ascending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.