Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Make it do just the essential part. Split anything that has nothing to do with the operation you're trying to handle to other classes. Put those behind interfaces, so you can mock these in your unittests. This way you'll notice you don't have to test anything with file i/o in this class. In the class below I split up the function in the essential part, some file i/o and retrieving of settings. Even these settings have nothing to do with the current method you're trying to test. The method just needs verification on, for example, the extension, but it doesn't matter on how it does this.</p> <p>Tip: try to avoid static utility classes. Give them their own class. Also avoid external components such as network communication or file i/o.</p> <p>As I don't have a lot of context and it may not compile. But I would go with something like:</p> <pre><code>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 &gt; _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&lt;int, int&gt; GetThumbnailDimensions() { return Tuple.Create&lt;int, int&gt;(PageConstants.BRANCH_PROFILE_PICTURE_FILE_HEIGTH, PageConstants.BRANCH_PROFILE_PICTURE_FILE_WIDTH); } public int GetMaximumFileSize() { return PageConstants.PROFILE_PICTURE_FILE_SIZE; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload