如何构造HttpPostedFileBase?我必须为此方法编写单元测试,但我无法构造HttpPostedFileBase...当我从浏览器运行该方法时,它工作正常,但我确实需要一个自动单元测试。所以我的问题是:如何构造HttpPosterFileBase以便将文件传递给HttpPostedFileBase。谢谢。publicActionResultUploadFile(IEnumerablefiles){foreach(varfileinfiles){//...}}做这样的事情怎么样:publicclassMockHttpPostedFileBase:HttpPostedFileBase{publicMockHttpPostedFileBase(){}}然后你可以创建一个新的:MockHttpPostedFileBasemockFile=newMockHttpPostedFileBase();在我的例子中,我通过asp.netMVCweb界面和RPCwebservice和unittest使用核心注册核心。在这种情况下,为HttpPostedFileBase定义一个自定义的包装器是很有用的:以上是C#学习教程:HowtoconstructHttpPostedFileBase?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——publicclassHttpPostedFileStreamWrapper:HttpPostedFileBase{string_contentType;字符串_文件名;流_输入流;publicHttpPostedFileStreamWrapper(StreaminputStream,stringType=stringcontentnull,stringfilename=null){_inputStream=inputStream;_contentType=内容类型;_filename=文件名;}publicoverrideintContentLength{get{return(int)_inputStream.Length;}}publicoverridestringContentType{get{return_contentType;//////摘要:///获取客户端上文件的完全限定名称。///返回:///客户端文件的名称,包括目录路径。///publicoverridestringFileName{get{return_filename;}}publicoverrideStreamInputStream{get{return_inputStream;}}publicoverridevoidSaveAs(stringfilename){using(varstream=File.OpenWrite(文件名)){InputStream.CopyTo(流);}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
