2011-06-02 155 views
0

我對測試還比較陌生,並且仍然對我的某些基礎知識有所瞭解。我有一個方法,我想測試它基本上創建一個不同的文件名,如果提供的已經存在(我粘貼下面的代碼)。測試FileNotFound處理的最佳方法

我需要一種測試方法,如果該文件已經存在,該方法會返回一個不同的(但也是唯一的)名稱。在Visual Studio的單元測試中測試這個最好的方法是什麼?它是創建一個文件作爲測試的一部分,然後刪除它還是有更好的方法?

public static FileInfo SafeFileName(this FileInfo value) 
{ 
    if (value == null) throw new ArgumentNullException("value"); 

    FileInfo fi = value; 

    //Check the directory exists -if it doesn't create it as we won't move out of this dir 
    if (!fi.Directory.Exists) 
     fi.Directory.Create(); 

    //It does so create a new name 
    int counter = 1; 
    string pathStub = Path.Combine(fi.Directory.FullName, fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length)); 

    // Keep renaming it until we have a safe filename 
    while (fi.Exists) 
     fi = new FileInfo(String.Concat(pathStub, "_", counter++, fi.Extension)); 

    return fi; 
} 
+0

你可以測試所有你想要的,但這永遠不會是併發安全的。 – 2011-06-02 10:56:43

+0

感謝亨克,但這不是特別關注的場景 – Tim 2011-06-03 10:18:52

回答

2

下面是兩種方案2的測試方法(使用的Visual Studio單元測試項目):

// using System.IO; 

    [TestMethod] 
    public void WhenFileExists() 
    { 
     // Create a file 
     string existingFilename = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); 
     using (File.Open(existingFilename, FileMode.CreateNew)) { } 

     // Check its existence 
     Assert.IsTrue(File.Exists(existingFilename)); 

     // Call method to be tested 
     string newFilename = DummyCreateFile(existingFilename); 

     // Check filenames are different 
     Assert.AreNotEqual<string>(existingFilename, newFilename); 

     // Check the new file exists 
     Assert.IsTrue(File.Exists(newFilename)); 
    } 

    [TestMethod] 
    public void WhenFileDoesNotExist() 
    { 
     // Get a filename but do not create it yet 
     string existingFilename = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); 

     // Check the file does not exist 
     Assert.IsFalse(File.Exists(existingFilename)); 

     // Call method to be tested 
     string newFilename = DummyCreateFile(existingFilename); 

     // Check the file was created with the filename passed as parameter 
     Assert.AreEqual<string>(existingFilename, newFilename); 

     // Check the new file exists 
     Assert.IsTrue(File.Exists(newFilename)); 
    } 

    private string DummyCreateFile(string filename) 
    { 
     try 
     { 
      using (File.Open(filename, FileMode.CreateNew)) { } 
      return filename; 
     } 
     catch 
     { 
      string newFilename = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); 
      using (File.Open(newFilename, FileMode.CreateNew)) { } 
      return newFilename; 
     } 
    } 

的測試方法稍有改變的,因爲它需要(並返回)一出於簡單的原因,而不是FileInfo的字符串參數。

+0

謝謝Sorin,請問這不是最終創建一堆文件雖然(即每次運行測試一個)? – Tim 2011-06-03 10:17:55

+0

是的,但是,文件是在Windows的臨時目錄中創建的,它們應該在某個時候被系統刪除。當然你也可以在退出測試方法之前確認並添加一些代碼以刪除它們。 – 2011-06-03 10:48:11

+0

謝謝索林,這是一個很大的幫助 – Tim 2011-06-03 19:12:10

3

我認爲更好的辦法是使用.NET運行庫:

System.IO.Path.GetRandomFileName() 

和擺脫的文件名生成代碼都在一起。

GetRandomFileName

+0

感謝Ritch我們使用臨時文件名,但是我們需要在這個特定場景中維護一個命名約定(這是爲了將數據導入到另一個系統中) – Tim 2011-06-03 10:16:29