Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

438
Views
Unable to mock SSH.Net's SFTPClient object for Microsofts Unit test case framework

I have used SSH.Net's SftpClient object to perform upload and dowload operations on the server.

I am trying to write unit test cases for the class that uses this SftpClient object.

The class itself is mocked but I am unable to setup a client object without actually having to connect to the server, which is not right.

public class MockFTPStreamRepository : IFTPStreamRepository
{
   public async Task<MySFTFReposResults> MyMethod1(MyDTO dto)
   {
     MemoryStream stream = new MemoryStream();
     StreamWriter writer = new StreamWriter(stream);
     SftpClient myClient = new SftpClient("ftp://localhost", "", "");
     sftpClient.Connect();
     writer.Write("Some Content");
     writer.Flush();
     stream.Position = 0;
     return new MySFTFReposResults { IsTransferSuccess = true, FileStream = stream, sftpClient = myClient};
     }
}

I cannot give an actual ftp server and credentials above to connect as that wont hold for other environments.

Is there a way to get a valid SftpClient object from this ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As this is a concrete class the testing of your methods cannot be done without creating a real connection. There are however multiple ways you could test this.

The first one is by wrapping the SftpCLient in a interface and use Constructor Injection to create a seam. Then with the Moq framework you can mock the method calls you call from within your own methods.

You could also create a local sftp server and create a mirror of the real-world cases so you can test the behavior of your methods.

It could also be that there is an nuget package that support in-memory SFTP server.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!