1

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 enter image description here Or, if you can give me another approach to follow it's will be great from you.

Thanks

Le-Mr-Ruyk
  • 179
  • 2
  • 17
  • 1
    What's the type of `file.filePath.InputStream`? Is there a reason it can't just be a `Stream`? – Nick Bailey Nov 28 '16 at 16:36
  • @NickBailey It's a HttpPostedFileBase as you can see above – Le-Mr-Ruyk Nov 28 '16 at 16:49
  • The more you tightly couple your code to external dependencies the harder your make it for you to unit test. the difficulty in testing your code is directly reflects how well designed your code is. If you code depends on implementation concerns then you are setting your self up for problems when you try to fake them up for testing – Nkosi Nov 28 '16 at 17:01
  • Create a Stream derived object and pass that as the result of the mock. there is actually no need for the System Wrapper. OpenXml should be treated as an external dependency. – Nkosi Nov 28 '16 at 17:02
  • Provide more context about the method under test. Not enough information to properly help other than using a stream – Nkosi Nov 28 '16 at 17:07
  • @Nkosi You can look above I put a snippet of my controller with all the logic needed, and the goal of my function. Crodially – Le-Mr-Ruyk Nov 28 '16 at 17:29
  • There is anyone who can help ? – Le-Mr-Ruyk Dec 06 '16 at 10:26

1 Answers1

0

You can mock it by setting up the InputStream to return the BaseStream of a StreamReader to a test file, e.g.:

var mockPostedFileBase = new Mock<HttpPostedFileBase>();
mockPostedFileBase.SetupGet(s => s.InputStream).Returns(new StreamReader(/* path to test file */).BaseStream);
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64