【发布时间】:2018-06-28 04:31:20
【问题描述】:
我正在尝试模拟请求以在以下控制器中测试文件上传功能:
public HttpResponseMessage Upload()
{
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0 &&
httpRequest.Params != null &&
httpRequest.Params.GetValues("param1") != null &&
httpRequest.Params.GetValues("param1")[0] != null &&
httpRequest.Params.GetValues("param2") != null &&
httpRequest.Params.GetValues("param2")[0] != null)
{
var postedFile = httpRequest.Files[0];
//do something
}
}
这是我的测试方法
[TestMethod]
public void CheckSuccessfulUpload()
{
//arrange
const string fileUploadXML = "<?xml version=\"1.0\" encoding =\"utf8\"?>" +
"<employees><employee id=\"1\" name=\"A\">" +
"<employees><employee id=\"2\" name=\"B\">" +
"<employees><employee id=\"3\" name=\"C\">" +
"</employees>";
//Create the file here to upload
//Set a variable to the My Documents path.
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
File.WriteAllText(mydocpath + @"\Employees.xml", fileUploadXML);
//Mock the Request
HttpRequest request = new HttpRequest(mydocpath + @"\Employees.xml", "http://localhost/api/DataUpload", "param1=1¶m2=2");
HttpResponse response = new HttpResponse(new StringWriter());
HttpContext.Current = new HttpContext(request, response);
//action
DataUploadController controller = new DataUploadController();
controller.upload();
//this is sending the parameters but not the file
}
我能够成功地使用参数而不是文件来命中控制器方法。尝试使用HttpRequestMessage、HttpClient 和MultipartFormDataContent,但它们都不起作用。也无法在网络上获得良好的参考。我也可以使用Mock/Moq 框架。
【问题讨论】:
-
检查stackoverflow.com/a/38170800/5233410,人们需要停止将他们的代码与
System.Web和HttpContext紧密耦合。他们远离它是有原因的。它不是很可测试。 -
所以我建议您重新考虑该控制器操作的设计,因为 Web api 是最近设计的,并且在 ApiController 中有更多可模拟的入口点。
-
是的,这将是理想的解决方案。在我完成项目级别更改之前,有没有其他方法可以模拟它并至少在短期内修复它?
-
不。 HttpContext 和 HttpRequest 是密封的。所以那里没有运气。
-
使用抽象类
HttpContextBase而不是静态HttpContext,然后你就可以模拟它了。
标签: c# unit-testing asp.net-web-api file-upload