How to Moq a FtpDataStream
本文关键字:FtpDataStream Moq to How | 更新日期: 2023-09-27 18:06:38
我编写了一个FTP客户端应用程序,用nunit工具测试了我的功能。我尝试模拟FTP数据流。gettallcsvfilesfromftp方法调用FtpConnect()方法,但在测试时,我想获得流数据而不连接FTP服务器。
public List<string> GetAllCsvFilesFromFTP()
{
var files = new List<string>();
var responseStream = _ftpConnection.FtpConnect(WebRequestMethods.Ftp.ListDirectory);
if (responseStream != null)
{
var reader = new StreamReader(responseStream);
ReadResponseStream(files, reader);
reader.Close();
responseStream.Close();
}
return files.OrderByDescending(p => p).ToList();
}
public Stream FtpConnect(string connectionType, string fileName = "")
{
try
{
var request = (FtpWebRequest)WebRequest.Create(_readConfigXml.FtpUrl + fileName);
request.Credentials = new NetworkCredential(_readConfigXml.UserName, _readConfigXml.PassWord);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
request.Method = connectionType;
var response = (FtpWebResponse) request.GetResponse();
var stream = response.GetResponseStream();
return stream;
}
catch (Exception ex)
{
_logWriter.WriteLog(_readConfigXml.LogPath, ex.Message);
return null;
}
}
我试着模拟测试的设置方法如下:_ftpConnection。设置(m => m.FtpConnect (It.IsAny (), It.IsAny ())) .Returns (?),
你能告诉我如何模拟退货数据吗?
string csvdata = "1,2,3,4,5":
var ms = new MemoryStream(Encoding.UTF8.GetBytes(csvdata));
ms.Position = 0;
_ftpConnection.Setup(m => m.FtpConnect(It.IsAny(), It.IsAny())).Returns(ms);