2010-08-06 13 views
5

Bu yöntem için bir Birim sınaması yazmam gerekiyor ancak HttpPostedFileBase yapılamıyor ... Bu yöntemi tarayıcıdan çalıştırdığımda iyi çalışıyor ancak gerçekten bunun için bir otomatik birim testine ihtiyacım var. Bu yüzden sorum şu: HttpPostedFileBase'e bir dosya aktarmak için HttpPosterFileBase'i nasıl yapılandırabilirim?HttpPostedFileBase nasıl oluşturulur?

Teşekkürler.

public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> files) 
    { 
     foreach (var file in files) 
     { 
      // ... 
     } 
    } 

cevap

6

böyle bir şey yaptığını nereden: asp.net MVC aracılığıyla Benim durumumda

MockHttpPostedFileBase mockFile = new MockHttpPostedFileBase(); 
2

, kullandığım çekirdek kaydı çekirdeğini:

public class MockHttpPostedFileBase : HttpPostedFileBase 
{ 
    public MockHttpPostedFileBase() 
    { 

    } 
} 

o zaman yeni bir tane oluşturabilirsiniz web arayüzü ve RPC web servisiyle ve en hızlı şekilde. Bu durumda, 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> 
    /// 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. 
    /// </summary>  
    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); 
     } 
    } 
için özel sarmalayıcı tanımlaması yararlıdır