VS 2010单元测试和无符号类型

时间:2009-07-09 06:14:09

标签: c# unit-testing nunit migration mstest

我正在将我的单元测试从NUnit转换为VisualStudio 2010单元测试框架。以下ByteToUShortTest()方法失败,并显示以下消息:

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]);
}

1 个答案:

答案 0 :(得分:4)

不是那些不同的类型,而是实例。你正在比较两个不同的阵列。