Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

437
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda