将图像上传到 Cloud UnitTest
我正在尝试将图像上传到云端,并且希望能够对此进行测试, 这是我已经尝试过的,我不太明白我在做什么,所以如果有人能告诉我该怎么做,我将不胜感激。
到目前为止,我已经包含了该方法的主要方法以及该方法的测试。
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.");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说这并不是真正的单元测试 - 您正在尝试将某些内容上传到您无法控制的外部服务,并且无法保证每次运行的结果都是相同的。
您编写的是集成测试,它是对两个或多个软件组件如何协同工作的测试。在这种情况下,这两个组件是
集成测试没有任何问题,但它们往往会更慢(在这种情况下,由于将文件上传到云),而且它们往往更脆弱。例如,如果云服务不可用,您的集成测试就会中断。您的代码没有任何变化,您的测试也没有任何变化,但测试的结果不同。
如果您想对
UploadToCloud
方法进行单元测试,我建议您首先将“云上传”功能包装在实现接口的类中,例如ICloudUploader
。然后,您可以模拟实际与云服务通信的部分,并确保您的代码的功能在您想要测试的所有情况下(成功上传、服务不可用、上传由于文件太大而失败,无论如何)。要模拟一个类,您可以推出自己的类(编写一个实现您的接口的类,例如
public class FakeCloudUploader : ICloudUploader
,或者查看类似 Moq 或 UploadToCloud 返回的字符串。您期望的值。
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
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.