简体   繁体   English

将Moq与SystemWrapper结合使用以对Action c#MVC进行单元测试

[英]Using Moq with SystemWrapper To unit testing an Action c# MVC

I'm trying to test an action where I have an InputStream, and I don't really know how to procede. 我正在尝试测试具有InputStream的操作,但我真的不知道该如何进行。

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. 我正在尝试模拟该类,但我注意到我们无法模拟System.IO,因为我们没有抽象层。

So, after some searchs, I'm oriented to use a package that add a abstraction layer of the IO system and wrap it. 因此,在进行一些搜索之后,我打算使用一个添加IO系统抽象层并将其包装的包。 is "SystemWrapper", but I didn't successed to use it. 是“ SystemWrapper”,但我没有成功使用它。

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. 我的目标 检查Excel文件头是否与canva格式相同(如果期望的格式为true,否则为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 谢谢

You can mock it by setting up the InputStream to return the BaseStream of a StreamReader to a test file, eg: 您可以通过设置InputStream来模拟它,以将StreamReader的BaseStream返回到测试文件,例如:

var mockPostedFileBase = new Mock<HttpPostedFileBase>();
mockPostedFileBase.SetupGet(s => s.InputStream).Returns(new StreamReader(/* path to test file */).BaseStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM