开发者

How to Moq a FtpDataStream

开发者 https://www.devze.com 2023-03-31 10:31 出处:网络
I code a FTP client Application, testing my functions by nunit tool. I try to mock FTP data stream. GetAllCsvFilesFromFTP method call FtpConnect() method, but while testing I want to get a Stream data

I code a FTP client Application, testing my functions by nunit tool. I try to mock FTP data stream. GetAllCsvFilesFromFTP method call FtpConnect() method, but while testing I want to get a Stream data without connecting FTP server.

    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 f开发者_运维知识库ileName = "")
    {
        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;
        }
    }

I try to mock test’s Setup method in below: _ftpConnection.Setup(m => m.FtpConnect(It.IsAny(), It.IsAny())).Returns(?);

Could you help me about how can i simulate Returns data.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消