2017-02-15 51 views
0

我的DTO被宣告像下面單位測試方法的文件大小超過40MB

[MaxLength(maxFileSize, ErrorMessage = "Max Byte Array length is 40MB.")] 
    public byte[] DocumentFile { get; set; } 

我需要編寫單元測試方法對文件大小超過40MB以上。

由於DocumentFile屬性被聲明爲byte []數組類型,我無法將任何值賦給DocumentFile屬性。

任何人都可以請建議我如何編寫這種情況下的單元測試方法。

+4

該屬性包含'搞定;設置;',爲什麼你不能分配任何值的財產? – ColinM

+1

是你創建一個40MB + 1字節數組的問題嗎?或將其分配給該屬性?這從您的問題描述中不太清楚。它是否編譯失敗,在運行時失敗... – dlatikay

+0

當您嘗試設置大於40Mb的DocumentFile字節數組時,請捕獲該異常。預期異常 - 超出最大文件大小 - 這是此處測試的目標 - 您應該無法設置較大的文件,因爲它是有限的。 –

回答

3

既不編譯器也沒有運行時具有帶有40MB + 1名的字節數組的一個問題:

namespace so42248850 
{ 
    class Program 
    { 
     class someClass 
     { 
      /* [attributes...] */ 
      public byte[] DocumentFile; 
     } 

     static void Main(string[] args) 
     { 
      var oversized = new byte[41943041]; /* 40 MB plus the last straw */ 
      try 
      { 
       var mock = new someClass 
       { 
        DocumentFile = oversized 
       }; 
      } 
      catch(Exception e) 
      { 
       /* is this the expected exception > test passes/fails */ 
      } 
     } 
    } 
} 

我不推薦這樣的方法在生產大規模的多用戶場景中,因爲它可能會引起相當長的一段存儲器壓力,但對於自動化測試應該沒問題。

+0

優秀的解決方案,謝謝。 –

1

喜歡的東西

[TestMethod] 
[ExpectedException(typeof(BlaBlaException), "Exceptiion string")] 
public void DocumentFile_set_WhenDocumentFileSetOver40Mb_ShouldThrowExceptionBlaBla { 
    DocumentFile = new byte [45000000]; 
} 
+0

優秀的解決方案,謝謝。 –