是否有上传文件到ASP.NET的WebAPI一个单元测试的方法是什么? [英] Is there a unit-testable way to upload files to ASP.NET WebAPI?

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

问题描述

我在使用新的ASP.NET的WebAPI项目工作。我现在的任务是接受上传的文件。到目前为止,我已经使用TDD驶出的WebAPI code,但我已经打上传视频墙。我目前在如下<一个发现的建议href=\"http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2\">http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2,但似乎没有办法在所有推动这一出单元测试。为了得到在文件和表单数据,我必须使用 MultipartFormDataStreamProvider ,这是不可能的嘲笑和/或覆盖。短放弃自己的TDD方法,我能做什么?

I'm working in a project that uses the new ASP.NET WebAPI. My current task is to accept an uploaded file. So far, I have used TDD to drive out the WebAPI code, but I've hit a wall with uploading. I'm currently following the advice found at http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2, but there seems to be no way at all to drive this out of a unit test. In order to get at the file and form data, I have to use MultipartFormDataStreamProvider, which is impossible to mock and/or override. Short of forsaking my TDD approach, what can I do?

下面的例子中的code:

Here's the code from the example:

public Task<HttpResponseMessage> PostFormData()
{
    // Check if the request contains multipart/form-data.
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);

    // Read the form data and return an async task.
    var task = Request.Content.ReadAsMultipartAsync(provider).
        ContinueWith<HttpResponseMessage>(t =>
        {
            if (t.IsFaulted || t.IsCanceled)
            {
                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
            }

            // This illustrates how to get the file names.
            foreach (MultipartFileData file in provider.FileData)
            {
                Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                Trace.WriteLine("Server file path: " + file.LocalFileName);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        });

    return task;
}

第一个问题是这一行:

The first problem is this line:

var provider = new MultipartFormDataStreamProvider(root);

对于初学者来说,单元测试这个code,我需要能够注入这样的供应商。它在简单的构造函数调用的方式太多要排队newing它。我们有了另一种方式。 (如果没有,失败的WebAPI)

For starters, to unit test this code, I need to be able to inject such a provider. It does WAY too much in that simple constructor call to be "newing it up" in line. There's got to be another way. (If not, WebAPI fails)

推荐答案

答案是没有。 ASP.NET是基于继承的框架。如果你想编写基于合成的应用程序,你会在某个时候,发现摩擦和路障。时间切换到像南希

The answer is "no". ASP.NET is an inheritance-based framework. If you're trying to write composition-based applications, you will, at some point, find friction and road-blocks. Time to switch to something like Nancy.

这篇关于是否有上传文件到ASP.NET的WebAPI一个单元测试的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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