如何构造HttpPostedFileBase?
我必须为此方法编写unit testing,但我无法构造HttpPostedFileBase …当我从浏览器运行该方法时,它运行良好,但我真的需要一个自动unit testing。 所以我的问题是:我如何构造HttpPosterFileBase以便将文件传递给HttpPostedFileBase。
谢谢。
public ActionResult UploadFile(IEnumerable files) { foreach (var file in files) { // ... } }
做这样的事情怎么样:
public class MockHttpPostedFileBase : HttpPostedFileBase { public MockHttpPostedFileBase() { } }
然后你可以创建一个新的:
MockHttpPostedFileBase mockFile = new MockHttpPostedFileBase();
在我的例子中,我通过asp.net MVC web界面和RPC webservice以及unittest使用核心注册核心。 在这种情况下,为HttpPostedFileBase定义自定义包装器很有用:
public class HttpPostedFileStreamWrapper : HttpPostedFileBase { string _contentType; string _filename; Stream _inputStream; public HttpPostedFileStreamWrapper(Stream inputStream, string contentType = null, string filename = null) { _inputStream = inputStream; _contentType = contentType; _filename = filename; } public override int ContentLength { get { return (int)_inputStream.Length; } } public override string ContentType { get { return _contentType; } } /// /// Summary: /// Gets the fully qualified name of the file on the client. /// Returns: /// The name of the file on the client, which includes the directory path. /// public override string FileName { get { return _filename; } } public override Stream InputStream { get { return _inputStream; } } public override void SaveAs(string filename) { using (var stream = File.OpenWrite(filename)) { InputStream.CopyTo(stream); } }
- 当缺少asp.net mvc的参数时自动抛出404错误
- asp.net core 2.0中System.Web.Hosting.HostingEnvironment.RegisterObject的替代方案
- IHttpModule和控制台应用程序的流畅NHibernate模式
- 在MVC4中添加控制器不起作用
- PagedListPager传递其他模型数据
- ASP.Net应用程序将powershell脚本作为IIS_USR执行
- MVCvalidation – 使用服务层保持干燥 – 最佳做法是什么?
- entity framework – 检查挂起的迁移
- ASP.NET MVC公共替代UrlHelper.GenerateUrl