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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With