Question

I am trying to upload an image to a cloud and i want to be able to test this, here is what i have tried already, I dont really understand fully what im doing, so if somebody could tell me what to do, I would appreciate it.

I have included the main method for this and the test of that method so far.

public static String UploadToCloud(string fileName)
    {
        try
        {
            SetUpConnection();
            #region Upload a File from local storage to the Cloud
            // Get a reference to the blob.
            blob = blobContainer.GetBlobReference("Images/" + fileName.Substring(fileName.LastIndexOf('\\')));
            blob.UploadFile(fileName);
            return blob.Uri.ToString();
            #endregion
        }
        catch (StorageClientException e)
        {
            Console.WriteLine("Storage client error encountered: " + e.Message);
            return "Upload failed";
        }
    }

/// <summary>
    ///A test for UploadToCloud
    ///</summary>
    [TestMethod()]
    public void UploadToCloudTest()
    {
        string fileName = "https://kevin.blob.core.windows.net/cp300/Images//skin-mole.jpg";
        Image expected = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\skin-mole.jpg");
        string actual;
        actual = CloudConnection.UploadToCloud(fileName);

        //Compares to images and checks they are exactly the same
        MemoryStream ms = new MemoryStream();
        expected.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        String expectedBitmap = Convert.ToBase64String(ms.ToArray());
        ms.Position = 0;
        actual.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        String actualBitmap = Convert.ToBase64String(ms.ToArray());

        Assert.AreEqual(expectedBitmap, actualBitmap); 

        //Assert.AreEqual(expected, actual);
        //Assert.Inconclusive("Verify the correctness of this test method.");
    }
Was it helpful?

Solution

I'd say that this isn't really a unit test -- you're trying to upload something to an external service that you have no control over and can't guarantee that the results are going to be the same from run to run.

What you've written is an integration test, which is a test of how two or more software components work together. In this case, the two components are

  • Your code
  • The cloud upload API

There's nothing wrong with integration tests, but they tend to be slower (in this case, due to uploading a file to the cloud), and they tend to be more brittle. Your integration test, for example, would break if the cloud service wasn't available. Nothing changed in your code, nothing changed in your test, but the test's results were different.

If you wanted to unit test your UploadToCloud method, I'd recommend that you start by wrapping your "cloud uploading" functionality in a class that implements an interface, e.g. ICloudUploader. Then you can mock out the pieces that actually communicate with your cloud service, and ensure that the functionality of your code is correct under all of the situations you want to test (successful upload, service is unavailable, upload fails due to file being too big, whatever).

For mocking out a class, you can either roll your own (write a class that implements your interface, for example public class FakeCloudUploader : ICloudUploader, or look into a mocking framework like Moq or RhinoMocks.

As for the test method you provided, it's not really testing the output of the method. It should validate that the string you get back from UploadToCloud is the value you expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top