2013-03-07 101 views
0

我正在爲此方法編寫單元測試。我嘗試了很多次,但仍然無法編寫任何代碼。請告訴我如何進行單元測試。我使用C#,nunit框架和rhino mock。如何編寫此方法的單元測試

在此先感謝。

 public FileUploadJsonResult AjaxUploadProfile(int id, string branchName, string filepath, HttpPostedFileBase file) 
    { 
     // TODO: Add your business logic here and/or save the file 
     string statusCode = "1"; 
     string profilePicture = string.Empty; 
     string fileExtension = System.IO.Path.GetExtension(file.FileName.ToLower()); 
     string fileName = id + "_" + branchName; 
     string fileNameWithOriginalExtension = fileName + fileExtension; 
     string fileNameWithJPGExtension = fileName + ".jpg"; 
     string fileServerPath = this.Server.MapPath("~/LO_ProfilePicture/" + fileNameWithJPGExtension); 
     string statusMessage = string.Empty; 
     if (string.IsNullOrEmpty(fileExtension) || !Utility.isCorrectExtension(fileExtension)) 
     { 
      statusMessage = "Profile picture should be of JPG, BMP, PNG, GIF or JPEG format."; 
      return new FileUploadJsonResult { Data = new { message = string.Format(statusMessage, fileNameWithOriginalExtension), filename = string.Empty, profilepic = profilePicture, statusCode = "0" } }; 
     } 
     if (file.ContentLength > PageConstants.PROFILE_PICTURE_FILE_SIZE) 
     { 
      statusMessage = "Profile picture size should be less than 2MB"; 
      return new FileUploadJsonResult { Data = new { message = string.Format(statusMessage, fileNameWithOriginalExtension), filename = string.Empty, profilepic = profilePicture, statusCode = "0" } }; 
     } 
     Utility.SaveThumbnailImage(fileServerPath, file.InputStream, PageConstants.BRANCH_PROFILE_PICTURE_FILE_HEIGTH, PageConstants.BRANCH_PROFILE_PICTURE_FILE_WIDTH); 
     profilePicture = PageConstants.IMAGE_PATH + "LO_ProfilePicture/" + fileNameWithJPGExtension; 
     // Return JSON    
     return new FileUploadJsonResult { Data = new { message = string.Format("Profile Picture is successfully uploaded.", fileNameWithOriginalExtension), filename = fileNameWithJPGExtension, profilepic = profilePicture, statusCode } }; 
    } 
+1

不是每種方法都可以進行單元測試。如果你可以打破非Json邏輯(例如基於擴展/內容長度的決策樹,那麼單元測試_that_並保持Json部分很小) – 2013-03-07 17:13:41

+0

我想刪除所有代碼並重新開始。如果你使用的是TDD,你不會在這個壞的附近寫任何東西 – 2013-03-07 17:16:35

+3

@DStanley我不同意不是每個方法都可以進行單元測試如果它不是單元測試的,那麼你的代碼很可能會破壞SRP並且需要重構以便它可以被單元測試 – 2013-03-07 17:17:27

回答

1

做它只是必不可少的部分。將與您嘗試處理的操作無關的任何內容拆分爲其他類。把這些放在界面後面,這樣你可以在你的單元測試中嘲笑這些。這樣你會注意到你不必在這個類中用文件I/O來測試任何東西。在下面的課程中,我分解了基本部分中的函數,一些文件I/O和檢索設置。即使這些設置與您嘗試測試的當前方法無關。該方法只需要對擴展進行驗證,但對於如何執行此操作無關緊要。

提示:儘量避免靜態工具類。給他們自己的班級。還要避免外部組件,如網絡通信或文件I/O。

由於我沒有很多的上下文,它可能無法編譯。但我會用類似的東西:

class Controller { 
    public FileUploadJsonResult AjaxUploadProfile(int id, string branchName, string filepath, HttpPostedFileBase file) { 
     string fileName = id + "_" + branchName; 
     string fileExtension = _fileIO.GetExtensionForFile(file); 

     if (!_extensionManager.IsValidExtension(fileExtension)) { 
      return CreateAjaxUploadProfileError("Profile picture should be of JPG, BMP, PNG, GIF or JPEG format."); 
     } 

     if (file.ContentLength > _settingsManager.GetMaximumFileSize()) { 
      return CreateAjaxUploadProfileError("Profile picture size should be less than 2MB"); 
     } 

     string fileNameWithJPGExtension = fileName + ".jpg"; 
     string fileServerPath = _fileIO.GetServerProfilePicture(Server, fileNameWithJPGExtension); 
     string fileClientPath = _fileIO.GetClientProfilePicture(fileNameWithJPGExtension); 

     var dimensions = _settingsManager.GetThumbnailDimensions(); 
     _fileIO.SaveThumbnailImage(fileServerPath, file, dimensions.Item1, dimensions.Item2); 

     // Return JSON  
     var data = new { 
       message = "Profile Picture is successfully uploaded.", 
       filename = fileClientPath, 
       profilepic = profilePicture, 
       statusCode = "1" 
      }; 
     return new FileUploadJsonResult { Data = data }; 
    } 

    private static CreateAjaxUploadProfileError(string message) { 
     var data = new { 
       message = message, 
       filename = string.Empty, 
       profilepic = string.Empty, 
       statusCode = "0" 
      }; 
     return new FileUploadJsonResult { Data = data }; 
    } 
} 

class FileIO : IFileIO { 
    public string GetExtensionForFile(HttpPostedFileBase file) { 
     return System.IO.Path.GetExtension(filePath.FileName.ToLower()); 
    } 

    public string GetServerProfilePicture(T server, string file) { 
     return server.MapPath("~/LO_ProfilePicture/" + file); 
    } 

    public void SaveThumbnailImage(string path, HttpPostedFileBase file, int height, int width) { 
     Utility.SaveThumbnailImage(path, file.InputStream, height, width); // or even inline 
    } 

    public string GetClientProfilePicture(string fileName) { 
     return _settingsManager.GetClientImagePath() + "LO_ProfilePicture/" + fileNameWithJPGExtension; 
    } 
} 

class ExtensionManager : IExtensionManager { 
    public bool IsValidExtension(string extension) { 
     return Utility.isCorrectExtension(fileExtension); // or even inline 
    } 
} 

class SettingsManager : ISettingsManager { 
    public Tuple<int, int> GetThumbnailDimensions() { 
     return Tuple.Create<int, int>(PageConstants.BRANCH_PROFILE_PICTURE_FILE_HEIGTH, PageConstants.BRANCH_PROFILE_PICTURE_FILE_WIDTH); 
    } 

    public int GetMaximumFileSize() { 
     return PageConstants.PROFILE_PICTURE_FILE_SIZE; 
    } 
} 
1

你可以看看這個函數作爲多個功能的組合執行特定的工作。一個功能是獲取目標文件路徑,另一個正在驗證擴展,另一個正在驗證大小,另一個正在創建縮略圖等。

目標是將複雜代碼分解爲可以獨立測試的小型可測試函數(單元)。所以當你把它們放在一起時,你更有信心你的大功能按預期工作。

相關問題