针对Mock文件名编写测试

时间:2018-06-29 06:34:31

标签: c# unit-testing moq

我有一个方法,该方法从Windows窗体的后台代码中调用,它接受string[]文件名。为ReadFile()编写单元测试时,我感到内。由于我没有办法模拟文件名。如果可以模拟文件名,请提供帮助

这是方法

 public Results ImportData(string[] fileNames)
        {
            List<ListViews> listViewDataItems = new List<ListViews>();

            try
            {
                foreach (var filename in fileNames)
                {

                    var readResults = ReadFile(filename);//unit test for this
                    var mapResults = MappingData(readResults);
                    var listViewDataItemsMiniBucket = ImportToDatabase(mapResults, filename);
                    listViewDataItems.Add(listViewDataItemsMiniBucket);
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.Instance.SendErrorToFile(ex);
                return new Results
                {
                    DescripText = ex.ToString(),
                    Success = false
                };
            }
            return new Results { Success = true, Objects = listViewDataItems };
        }

文件名功能

 public DataSet ReadFile(string filePath)
        {
            using (var stream = new FileStream(filePath, FileMode.Open))
            {
                var reader = GetExcelDataReaderExtension(stream, filePath);

                using (reader)
                {
                    return reader.AsDataSet(new ExcelDataSetConfiguration()
                    {
                        ConfigureDataTable = _ => new ExcelDataTableConfiguration()
                        {
                            FilterRow = rowReader => rowReader.Depth > Constants.HeaderIndex
                        }
                    });
                }
            }
        }

下面的单元测试

  [Fact]
        public void ReadTest()
        {

            Import test = new Import();
            var filePath = Directory.GetCurrentDirectory() + "\\Files\\";
            var fileName = "Refund Cases Cancelled in November 2016 - filenameCopy.xlsx";
            var file = filePath + fileName;

            var readResults = test.ReadFile(file);
            Assert.False(readResults.HasErrors);
        }

单元测试ImportData()

 [Fact]
        public void ImportData()
        {
            Import test = new Import();
            var filePath = Directory.GetCurrentDirectory() + "\\Files\\";

            var files = new[]
            {
             filePath+"Refund Cases Cancelled in November 2016 - Copy - Copy.xlsx",
             filePath+"Refund Cases Cancelled in November 2016 - Copy - Copy.xlsx"
            };
            var results = test.ImportData(files).Success;

            Assert.True(results);
        }

0 个答案:

没有答案