I'm trying to test an action where I have an InputStream, and I don't really know how to procede.
I'm trying with mocking the class and I get notice that we cannot Mock the System.IO because we don't have an abstraction layer.
So, after some searchs, I'm oriented to use a package that add a abstraction layer of the IO system and wrap it. is "SystemWrapper", but I didn't successed to use it.
My goal: Is to check Excel file headers if it's the same as the canva format if the format excpected it will be true else false.
There is my code :
Function that I want to test:
public bool checkFileHeaders(UploadedFile file)
{
ExcelPackage package = new ExcelPackage(file.filePath.InputStream);
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
List<string> expectedArray = new List<string>();
List<string> array = new List<string>();
bool checkExprectedArrayWithReelArray = true;
switch (file.typeFile)
{
case "headcount":
expectedArray.AddRange(new string[] { "MLE", "NOM", "PRENOM", "type" });
try
{
array.AddRange(new string[] { (string)worksheet.Cells["A1"].Value, (string)worksheet.Cells["B1"].Value, (string)worksheet.Cells["C1"].Value, (string)worksheet.Cells["D1"].Value});
}
catch
{
checkExprectedArrayWithReelArray = false;
}
case "File2":
expectedArray.AddRange(new string[] { "Field1", "Field2", "Field3" });
try
{
array.AddRange(new string[] { (string)worksheet.Cells["A1"].Value, (string)worksheet.Cells["B1"].Value, (string)worksheet.Cells["C1"].Value});
}
catch
{
checkExprectedArrayWithReelArray = false;
}
}
checkExprectedArrayWithReelArray = Enumerable.SequenceEqual(expectedArray.OrderBy(t => t), array.OrderBy(t => t));
package.Dispose();
return checkExprectedArrayWithReelArray;
}
Test Function :
[TestMethod]
public void CheckFileHeaders_STCWithExpectedFormat_ShouldReturnTrue()
{
var mockFileStream = new Mock<FileStreamWrap>();
var _mockHttpStream = new Mock<HttpPostedFileBase>();
_mockHttpStream.Setup(f => f.InputStream)
.Returns( ??????);
OpsReview.Core.Parsers.UploadedFile file = new OpsReview.Core.Parsers.UploadedFile
{
typeFile = "stc",
filePath = _mockHttpStream.Object
};
var result = _controller.checkFileHeaders(file);
result.Should().Be(true);
}
Type of my Input stream
Or, if you can give me another approach to follow it's will be great from you.
Thanks