模拟HttpPostedFileBase和InputStream进行单元测试

mos*_*o87 21 asp.net-mvc unit-testing moq httppostedfilebase

我想测试以下代码行:

...
Bitmap uploadedPicture = Bitmap.FromStream(model.Picture.InputStream) as Bitmap;
...
Run Code Online (Sandbox Code Playgroud)

Picture是我的模型类型HttpPostedFileBase中的属性.所以我想模拟一个HttpPostedFileBase属性进行单元测试:

model.Picture = new Mock<HttpPostedFileBase>().Object;
Run Code Online (Sandbox Code Playgroud)

没问题.

现在我必须模拟InputStream,否则它是null:

model.Picture.InputStream = new Mock<Stream>().Object;
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为InputStream是只读的(没有setter方法):

public virtual Stream InputStream { get; }
Run Code Online (Sandbox Code Playgroud)

是否有一个良好而干净的方法来处理这个问题?一种解决方案是在我的单元测试的派生类中重写HttpPostedFileBase.还有其他想法吗?

Tia*_*C13 34

嗨那里:)我做了类似的事,

    [TestInitialize]
    public void SetUp()
    {
        _stream = new FileStream(string.Format(
                        ConfigurationManager.AppSettings["File"],
                        AppDomain.CurrentDomain.BaseDirectory), 
                     FileMode.Open);

        // Other stuff
    }
Run Code Online (Sandbox Code Playgroud)

在测试本身,

    [TestMethod]
    public void FileUploadTest() 
    {
        // Other stuff

        #region Mock HttpPostedFileBase

        var context = new Mock<HttpContextBase>();
        var request = new Mock<HttpRequestBase>();
        var files = new Mock<HttpFileCollectionBase>();
        var file = new Mock<HttpPostedFileBase>();
        context.Setup(x => x.Request).Returns(request.Object);

        files.Setup(x => x.Count).Returns(1);

        // The required properties from my Controller side
        file.Setup(x => x.InputStream).Returns(_stream);
        file.Setup(x => x.ContentLength).Returns((int)_stream.Length);
        file.Setup(x => x.FileName).Returns(_stream.Name);

        files.Setup(x => x.Get(0).InputStream).Returns(file.Object.InputStream);
        request.Setup(x => x.Files).Returns(files.Object);
        request.Setup(x => x.Files[0]).Returns(file.Object);

        _controller.ControllerContext = new ControllerContext(
                                 context.Object, new RouteData(), _controller);

        // The rest...
    }
Run Code Online (Sandbox Code Playgroud)

希望这可以为您的解决方案提供一个想法:)


Dan*_*ake 13

我刚刚开始研究类似的东西,并希望将以下内容添加到@ TiagoC13的答案中.

我正在测试的系统是我正在编写的文件服务,其中一个要求是测试文件是否具有正确的尺寸.注意,硬编码的文件名.它作为我的测试项目中的文件夹和文件存在.该文件的属性如下所示:构建操作:嵌入式资源和复制到输出目录:如果较新则复制(尽管复制始终应该正常工作)

构建项目时,testimage.jpg及其文件夹将添加到bin中,然后测试会找到它.

还要注意fileStream.Close(); 这会释放文件,因此您可以在同一个套件中进行大量类似的测试.

希望这有帮助.

using Moq;
using NUnit.Framework;
using System.Web;

    [Test]
    public void IsValidFile() {
        string filePath = Path.GetFullPath(@"testfiles\testimage.jpg");
        FileStream fileStream = new FileStream(filePath, FileMode.Open);
        Mock<HttpPostedFileBase> uploadedFile = new Mock<HttpPostedFileBase>();

        uploadedFile
            .Setup(f => f.ContentLength)
            .Returns(10);

        uploadedFile
            .Setup(f => f.FileName)
            .Returns("testimage.jpg");

        uploadedFile
            .Setup(f => f.InputStream)
            .Returns(fileStream);

        var actual = fileSystemService.IsValidImage(uploadedFile.Object, 720, 960);

        Assert.That(actual, Is.True);

        fileStream.Close();
    }
Run Code Online (Sandbox Code Playgroud)


fea*_*net 9

无需通过在磁盘上打开文件来创建流.实际上我认为这是一个非常可怕的解决方案.可以在内存中轻松创建工作测试流.

var postedFile = new Mock<HttpPostedFileBase>();

using (var stream = new MemoryStream())
using (var bmp = new Bitmap(1, 1))
{
    var graphics = Graphics.FromImage(bmp);
    graphics.FillRectangle(Brushes.Black, 0, 0, 1, 1);
    bmp.Save(stream, ImageFormat.Jpeg);

    postedFile.Setup(pf => pf.InputStream).Returns(stream);

    // Assert something with postedFile here   
}        
Run Code Online (Sandbox Code Playgroud)

  • 这成为单元测试而不是作为Daniel解决方案的集成测试.这很好,我不知道你可以像这样推出这个,但这个不太可读(尽管我同意你可以在你的测试中实现代码重用).除了必须在测试项目中有一个图像之外,不确定另一个是多么可怕.它不比拥有集成测试数据库差...... (2认同)