单元测试一个文件上传,如何? [英] Unit Test a file upload, how?

查看:26
本文介绍了单元测试一个文件上传,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 MVC3.NET,我在控制器中有一个文件上传方法,该方法适用于以下签名 public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> file)

Using MVC3.NET I have a file upload method in a controller that works fine with the following signature public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> file)

如何使用 NUnit 进行单元测试?我环顾四周,似乎每个人都指向 Moq,但我是单元测试新手,无法让 Moq 正常工作.

How can I unit test this with NUnit? I have looked around and everyone seems to point to Moq but I'm new to unit testing and cannot get Moq working.

我发现了一些有趣的博客,例如:http://danielglyde.blogspot.com/2011/07/tdd-with-aspnet-mvc-3-moq-and.html 但我正在努力弄清楚如何伪造"文件上传,并且我还担心我现在设法找到的很多起订量示例似乎已弃用其中的代码.

I have found interesting blogs such as this: http://danielglyde.blogspot.com/2011/07/tdd-with-aspnet-mvc-3-moq-and.html but am struggling to figure out how the same might be done to 'fake' a file upload, and am also wary that a lot on moq examples that I have managed to find now seem to have deprecated code in them.

我只是想知道如何模拟 HttpPostedFileBase,以便我可以使用 Moq 或其他方式测试我的上传代码 - 如果有人能给我一些关于如何执行此操作的代码示例,我将不胜感激.

I would simply like to know how I can simulate a HttpPostedFileBase so I can test my upload code, using Moq or otherwise - I would be really grateful if someone could give me some code examples on how to do this.

以下代码取自此处的其他示例:

The following code taken from other examples on here:

var file = new Mock<HttpPostedFileBase>();
            file.Setup(f => f.ContentLength).Returns(1);
            file.Setup(f => f.FileName).Returns("test.txt");

controller.upload(file);

当我尝试编译时产生以下错误:

generates the following error when I try to compile:

无法从Moq.Mock"转换为'System.Web.HttpPostedFileBase'

cannot convert from 'Moq.Mock' to 'System.Web.HttpPostedFileBase'

我现在已经将方法更改为采用单数 HttpPostedFileBase,而不是 IEnumerable,因为能够模拟"一个是我试图关注这个问题的目的.

I have changed the method to take a singular HttpPostedFileBase for now, rather than an IEnumerable, as being able to 'mock' one is what I'm trying to focus on for the purpose of this question.

推荐答案

假设一个标准的文件上传动作:

Assuming a standard file upload action:

[HttpPost]
public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> files)
{
    foreach (var file in files)
    {
        var filename = Path.Combine(Server.MapPath("~/app_data"), file.FileName);
        file.SaveAs(filename);
    }
    return View();
}

你可以这样测试它:

[Test]
public void Upload_Action_Should_Store_Files_In_The_App_Data_Folder()
{
    // arrange
    var httpContextMock = new Mock<HttpContextBase>();
    var serverMock = new Mock<HttpServerUtilityBase>();
    serverMock.Setup(x => x.MapPath("~/app_data")).Returns(@"c:workapp_data");
    httpContextMock.Setup(x => x.Server).Returns(serverMock.Object);
    var sut = new HomeController();
    sut.ControllerContext = new ControllerContext(httpContextMock.Object, new RouteData(), sut);

    var file1Mock = new Mock<HttpPostedFileBase>();
    file1Mock.Setup(x => x.FileName).Returns("file1.pdf");
    var file2Mock = new Mock<HttpPostedFileBase>();
    file2Mock.Setup(x => x.FileName).Returns("file2.doc");
    var files = new[] { file1Mock.Object, file2Mock.Object };

    // act
    var actual = sut.UploadFile(files);

    // assert
    file1Mock.Verify(x => x.SaveAs(@"c:workapp_datafile1.pdf"));
    file2Mock.Verify(x => x.SaveAs(@"c:workapp_datafile2.doc"));
}

显然,所有 HttpContext 设置部分都应外化为 可重用可以在单元测试的 [SetUp] 阶段调用的类,以准备被测主题的模拟上下文并避免在每个单元测试中重复它.

Obviously all the HttpContext setup part should be externalized into a reusable class that could be called in the [SetUp] phase of your unit test to prepare the mock context of the subject under test and to avoid repeating it in every single unit test.

这篇关于单元测试一个文件上传,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆