Image我有一个从 a创建一个的类Byte[],以及其他一些逻辑,我正在尝试编写一个单元测试,断言Image从我的类返回的实例与我在单元测试中的一些假的相同Image。
我找不到可靠的方法来:
- 从一个假的Image\Byte[]资源\东西开始。
- Byte[]将代表假的东西传递给我的班级。
- 断言Image从我的班级返回的东西和我的假东西一样。
这是我到目前为止提出的代码:
Bitmap fakeBitmap = new Bitmap(1, 1);
Byte[] expectedBytes;
using (var ms = new MemoryStream())
{
    fakeBitmap.Save(ms, ImageFormat.Png);
    expectedBytes = ms.ToArray();
}
//This is where the call to class goes
Image actualImage;
using (var ms = new MemoryStream(expectedBytes))
    actualImage = Image.FromStream(ms);
Byte[] actualBytes;
using (var ms = new MemoryStream())
{
    actualImage.Save(ms, ImageFormat.Png);
    actualBytes = ms.ToArray();
}
var areEqual =
    Enumerable.SequenceEqual(expectedBytes, actualBytes);
Console.WriteLine(areEqual);
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
using(StreamWriter sw = new StreamWriter(Path.Combine(desktop, "expectedBytes.txt")))
    sw.Write(String.Join(Environment.NewLine, expectedBytes));
using(StreamWriter sw = new StreamWriter(Path.Combine(desktop, "actualBytes.txt")))
    sw.Write(String.Join(Environment.NewLine, actualBytes));
这总是返回false。