单元测试文件上传,怎么样? [英] Unit Test a file upload, how?

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

问题描述

使用MVC3.NET我有一个控制器文件上传方法具有以下签名正常工作公众的ActionResult UploadFile(IEnumerable的< HttpPostedFileBase>文件)

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的

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.

我发现有趣的博客像这样的:<一href=\"http://danielglyde.blogspot.com/2011/07/tdd-with-aspnet-mvc-3-moq-and.html\">http://danielglyde.blogspot.com/2011/07/tdd-with-aspnet-mvc-3-moq-and.html但我在努力弄清楚如何同样可能被做'假'的文件上传,并且我也警惕了很多,我已经设法现在找起订量的例子似乎在德precated code它们。

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这样我就可以测试我上传code,使用起订量或以其他方式 - 我会很感激,如果有人能够给我一些code示例如何做到这一点。

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.

从其他的例子在这里采取以下code:

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();
}

您可以测试它是这样的:

you could test it like this:

[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:\work\app_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:\work\app_data\file1.pdf"));
    file2Mock.Verify(x => x.SaveAs(@"c:\work\app_data\file2.doc"));
}

显然,所有的HttpContext设置部分应被外部化到可重用的类,可以在你的单元测试,以prepare主题的模拟上下文的 [设置] 阶段被称为测试,并避免在每次重复它单一的单元测试。

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天全站免登陆