1
我將單元測試從NUnit轉換到VisualStudio 2010單元測試框架。下面ByteToUShortTest()
方法失敗與消息:VS 2010單元測試和無符號類型
Assert.AreEqual failed. Expected:<System.UInt16[]>. Actual:<System.UInt16[]>.
[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
var array = new byte[2];
Assert.AreEqual(ByteToUShort(array), new ushort[1]);
}
由測試調用的代碼是:
[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array)
{
return ByteToUShort(array, 0, array.Length, EndianType.LittleEndian);
}
public enum EndianType
{
LittleEndian,
BigEndian
}
[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array, int offset, int length, EndianType endianType)
{
// argument validation
if ((length + offset) > array.Length)
throw new ArgumentException("The length and offset provided extend past the end of the array.");
if ((length % 2) != 0)
throw new ArgumentException("The number of bytes to convert must be a multiple of 2.", "length");
var temp = new ushort[length/2];
for (int i = 0, j = offset; i < temp.Length; i++)
{
if (endianType == EndianType.LittleEndian)
{
temp[i] = (ushort)(((uint)array[j++] & 0xFF) | (((uint)array[j++] & 0xFF) << 8));
}
else
{
temp[i] = (ushort)(((uint)array[j++] & 0xFF) << 8 | ((uint)array[j++] & 0xFF));
}
}
return temp;
}
該試驗成功地使用Nunit運行。任何想法爲什麼類型應該是不同的?
解
對於單和多維陣列,以及用於任何ICollection,所述的VisualStudio 2010單元測試框架提供了CollectionAssert類。
[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
var array = new byte[2];
CollectionAssert.AreEqual(ByteToUShort(array), new ushort[1]);
}